根据与dplyr的部分匹配,在数据框中的任意位置替换整个字符串 [英] Replace entire string anywhere in dataframe based on partial match with dplyr
问题描述
我正在努力寻找合适的dplyr代码以使用 grepl
或等效的代码来替换整个数据帧中的值.
I'm struggling to find the right dplyr code to use grepl
or an equivalent to replace values throughout an entire data frame.
即:其中包含"mazda"的任何单元格都应将其全部内容替换为新的字符串"A car"
i.e.: any cell that contains 'mazda' in it, should have it's entire content replaced with the new string 'A car'
经过大量的在线搜索,最接近我的是:
after lots of searching online, the closest I came was:
重点是将其应用于所有列.
The emphasis being on applying it to ALL columns.
library(dplyr)
mtcars$carnames <- rownames(mtcars) # dummy data to test on
这行代码可以使整个字符串完全匹配:
This line does the trick for entire sting being an exact match:
mtcars %>% replace(., (.)=='Mazda RX4', "A car")
但是出于某种原因,我的grepl尝试将整个列替换为"A car".
but my grepl attempt replaces the entire column with "A car" for some reason.
mtcars %>% replace(., grepl('Mazda', (.)), "A car")
推荐答案
library(dplyr)
mtcars %>% mutate_if(grepl('Mazda',.), ~replace(., grepl('Mazda', .), "A car"))
要了解为什么您第一次 replace
失败的原因,请参见'Mazda RX4'== mtcars
和 grepl('Mazda',mtcars)
,因为您使用的是 grepl
,所以 replace
使用
To understand why you first replace
failed see the difference between 'Mazda RX4'==mtcars
and grepl('Mazda', mtcars)
, since you used grepl
, replace
uses
replace用值中给定的索引替换list中给定的索引中的x中的值.如有必要,值中的值将被回收.
现在,如果我们确保例如使用 sapply
获得合适的输出,则可以使用您的第一种方法
Now we can use your first method if we make sure to get a suitable output using sapply
for example
mtcars %>% replace(., sapply(mtcars, function(.) grepl('Mazda',.)), "A car")
更新:
要替换多个模式,我们可以使用 stringr :: str_replace_all
TO replace multiple patterns we can use stringr::str_replace_all
library(stringr)
library(dplyr)
mtcars %>% mutate_if(str_detect(., 'Mazda|Merc'),
~str_replace_all(., c("Mazda.*" = "A car", "Merc.*" = "B car")))
这篇关于根据与dplyr的部分匹配,在数据框中的任意位置替换整个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!