0

I have data that belongs in a data frame, I want to change N/A values in "col1" where "col2" has text. Any "col1" N/A values which have text in "col2" I would like to change the "col1" value to "<1 Year":

Data:

col1    | col2
<1 YEAR | abcdefg
1 YEAR  | xyz
N/A     | abcde
N/A     | abc
N/A     |

This is the code I have tried for the first part:

df = mutate(df, col1 = ifelse(is.na(col1) && nchar(col2)>1,"lol",col1)) 
CJava
  • 169
  • 1
  • 4
  • 14
  • The `is.na` will not work if you have a character "N/A". Read the dataset with `na.strings = "N/A"` in `read.table/read.csv`.. Also, you need only `&` for this to work (assuming that the NA is real) – akrun Apr 07 '16 at 16:10
  • More directly `&&` is used for scalar logic whereas `&` is used for vectors. See http://stackoverflow.com/questions/6558921/r-boolean-operators-and – Chris Apr 07 '16 at 16:17

0 Answers0