dplyr替换所有与特定字符串匹配的变量 [英] dplyr to replace all variable which matches specific string
本文介绍了dplyr替换所有与特定字符串匹配的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在等效的 dplyr
呢?我在'全部替换'之后,将字符串xxx与NA匹配
Is there an equivalent dplyr
which does this? I'm after 'replace all' which matches string xxx with NA
is.na(df) <- df=="xxx"
我要执行 sparklyr
使用从R到Spark数据帧的管道功能命令。
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.
推荐答案
将 XXX替换为您要查找的字符串:
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))
此方法一次一地评估数据框中的每一列,并将该函数应用于查找 XXX并替换为不适用。
This method assesses each column in your data frame one-by-one and applies the function to lookup "XXX" and replace it with NA.
这篇关于dplyr替换所有与特定字符串匹配的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文