简体   繁体   English

使用带有 gsub 的 lapply 替换 dataframe 中的单词,使用另一个 dataframe 作为 'dictionnary'

[英]Using lapply with gsub to replace word in dataframe using another dataframe as 'dictionnary'

I have a dataframe called data where I want to replace some word in specific columns A & B.我有一个名为 dataframe 的数据,我想在其中替换特定列 A 和 B 中的一些单词。
I have a second dataframe called dict that is playing the role of dictionnary/hash containing the words and the values to use for replacement.我有第二个 dataframe 称为 dict,它扮演字典/哈希的角色,其中包含用于替换的单词和值。

I think it could be done with purrr's map() but I want to use apply.我认为可以使用 purrr 的 map() 来完成,但我想使用 apply。 It's for a package and I don't want to have to load another package.它适用于 package,我不想加载另一个 package。
The following code is not working but it's give you the idea.下面的代码不起作用,但它给了你想法。 I'm stuck.我卡住了。

columns <- c("A", "B" ) 
data[columns] <- lapply(data[columns], function(x){x}) %>% lapply(dict, function(y){
         gsub(pattern = y[,2], replacement = y[,1], x)})

This is working for one word to change...but I'm not able to pass the list of changes conainted in the dictionnary.这是为了改变一个词而工作......但我无法通过字典中包含的更改列表。

data[columns] <- lapply(data[columns], gsub, pattern = "FLT1", replacement = "flt1")

@Gregor_Thomas is right, you need a for loop to have a recursive effect, otherwise you just replace one value at the time. @Gregor_Thomas 是对的,你需要一个for循环来产生递归效果,否则你一次只能替换一个值。

df <- data.frame("A"=c("PB1","PB2","OK0","OK0"),"B"=c("OK3","OK4","PB1","PB2"))
dict <- data.frame("pattern"=c("PB1","PB2"), "replacement"=c("OK1","OK2"))

apply(df[,c("A","B")],2, FUN=function(x) {
  for (i in 1:nrow(dict)) {
    x <- gsub(pattern = dict$pattern[i], replacement = dict$replacement[i],x)
  }
  return(x)
})

Or, if your dict data is too long you can generate a succession of all the gsub you need using a paste as a code generator:或者,如果您的dict数据太长,您可以使用paste作为代码生成器生成一系列您需要的gsub

paste0("df[,'A'] <- gsub(pattern = '", dict$pattern,"', replacement = '", dict$replacement,"',df[,'A'])")

It generates all the gsub lines for the "A" column:它为“A”列生成所有gsub行:

"df[,'A'] <- gsub(pattern = 'PB1', replacement = 'OK1',df[,'A'])"
"df[,'A'] <- gsub(pattern = 'PB2', replacement = 'OK2',df[,'A'])"

Then you evaluate the code and wrap it in a lapply for the various columns:然后评估代码并将其包装在 lapply 中以用于各个列:

lapply(c("A","B"), FUN = function(v) { eval(parse(text=paste0("df[,'", v,"'] <- gsub(pattern = '", dict$pattern,"', replacement = '", dict$replacement,"',df[,'",v,"'])"))) })

It's ugly but it works fine to avoid long loops.它很难看,但可以很好地避免长循环。

Edit : for a exact matching between df and dict maybe you should use a boolean selection with == instead of gsub() .编辑:对于dfdict之间的精确匹配,也许你应该使用 boolean 选择和==而不是gsub() (I don't use match() here because it selects only the first matching (我在这里不使用match()因为它只选择第一个匹配

df <- data.frame("A"=c("PB1","PB2","OK0","OK0","OK"),"B"=c("OK3","OK4","PB1","PB2","AB"))
dict <- data.frame("pattern"=c("PB1","PB2","OK"), "replacement"=c("OK1","OK2","ZE"))

apply(df[,c("A","B")],2, FUN=function(x) {
for (i in 1:nrow(dict)) {
     x[x==dict$pattern[i]] <- dict$replacement[i]
    }
   return(x)
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM