Home > Mobile >  Understanding value assignment and data structure in R
Understanding value assignment and data structure in R

Time:10-27

Could someone explain to me why

df[df %in% c("a", "b", "c")] <- NA

does not work?

I have found a work around by using lapply and splitting the dataframe into a list of columns but I want to understand what is happening here. Thanks!

CodePudding user response:

The reason is that %in% works only on vector/single column and not on the whole dataset. We need

df[sapply(df, `%in%`, c('a', 'b', 'c'))] <- NA
  • Related