dplyr to replace all variable which matches specif

2019-08-23 18:25发布

Is there an equivalent dplyr which does this? I'm after 'replace all' which matches string xxx with NA

is.na(df) <- df=="xxx" 

I want to execute a sparklyr command using the pipe function from R to Spark dataframe

tbl(sc,"df") %>%

and sticking the first script above doesn't work.

1条回答
趁早两清
2楼-- · 2019-08-23 19:00

Replace "XXX" with the string you want to look for:

#Using dplyr piping
library(dplyr)
df[] = df %>% lapply(., function(x) ifelse(grepl("XXX", x), NA, x))

#Using only the base package
df[] = lapply(df, function(x) ifelse(grepl("XXX", x), NA, x))

This method assesses each column in your data frame one-by-one and applies the function to lookup "XXX" and replace it with NA.

查看更多
登录 后发表回答