Ian McTavish Ian McTavish - 22 days ago 6
R Question

Using R, How can i get a 'cell' in 'column1' to update when Column2 on the same row contains a word?

Using R, is there a regex way that I get a 'cell' in 'Column1' to update when 'Column2' on the same row contains a word within its contents?

For example# If I wanted cells that contain 'time' anywhere in them to then update the matrix' Column1 to '10' as below


Column1 Column2
10 that is the time
10 what time is the match
0 where is the car?


Currently I can get it to work for non regex (only if the exact cell contents is 'time' using the below;

ds1$test[ds1$Column2 %in% "time"] <- 10

Thanks in advance.

Answer

We can use grep

df1$Column1[grep("time", df1$Column2)] <- 10
Comments