[英]grepl in multiple columns in R
I'm trying to do a string search and replace across multiple columns in R. My code:我正在尝试跨 R 中的多个列进行字符串搜索和替换。我的代码:
# Get columns of interest
selected_columns <- c(368,370,372,374,376,378,380,382,384,386,388,390,392,394)
#Perform grepl across multiple columns
df[,selected_columns][grepl('apples',df[,selected_columns],ignore.case = TRUE)] <- 'category1'
However, I'm getting the error:但是,我收到错误:
Error: undefined columns selected
Thanks in advance.提前致谢。
grep/grepl
works on vectors/matrix and not on data.frame/list. According to the
grep/grepl
适用于向量/矩阵,而不适用于data.frame/list. According to the
data.frame/list. According to the
?grep` data.frame/list. According to the
?grep`
x - a character vector where matches are sought, or an object which can be coerced by as.character to a character vector. x - 寻找匹配的字符向量,或者可以被 as.character 强制转换为字符向量的对象。
We can loop over the columns ( lapply
) and replace
the values based on the match我们可以遍历列( lapply
)并根据匹配replace
值
df[, selected_columns] <- lapply(df[, selected_columns],
function(x) replace(x, grepl('apples', x, ignore.case = TRUE), 'category1'))
Or with dplyr
或者用dplyr
library(dplyr)
library(stringr)
df %>%
mutate_at(selected_columns, ~ replace(., str_detect(., 'apples'), 'category1'))
Assuming you want to partially match a cell and replace it, you could use rapply()
and replace cell contents that have "apples"
with "category1
" using gsub()
:假设您想部分匹配一个单元格并替换它,您可以使用rapply()
并使用gsub()
将具有"apples"
单元格内容替换为"category1
”:
df[selected_columns] <- rapply(df[selected_columns], function(x) gsub("apples", "category1", x), how = "replace")
Just keep in mind the difference between grepl()
/ gsub()
(with and without boundaries in your regex), and %in%
/ match()
when searching for strings.在搜索字符串时,请记住grepl()
/ gsub()
(在您的正则表达式中有边界和没有边界)和%in%
/ match()
之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.