0

Very simple question but I can't find an answer anywhere. I have a dataframe with column 'foo' which contains "A", "B", "C", "D", "F", and so on.

I need to recode every value that is not "A" or "B" to NA.

The code below doesn't work because operations like that aren't possible for strings... how do I accomplish this for strings?

df[df$foo != "A" | "B"] <- NA
Kasey
  • 173
  • 2
  • 11
  • Related to https://stackoverflow.com/questions/30098269/r-compare-multiple-values-with-vector-and-return-vector – thelatemail Mar 20 '19 at 03:05

1 Answers1

1

Try

df$foo[!(df$foo %in% c("A","B"))] <- NA

or

transform(df, foo=replace(foo, !(foo %in% c("A","B")), NA))
FKneip
  • 368
  • 3
  • 12