dplyr to replace all variable which matches specific string

北城以北 提交于 2019-12-12 04:06:20

问题


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:


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.



来源:https://stackoverflow.com/questions/45094830/dplyr-to-replace-all-variable-which-matches-specific-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!