提问者:小点点

R:如何提高grepl在数据帧中应用函数的性能


我有下面的数据框:

country<- c("CA","IN","US")
text   <- c("paint red green", "painting red", "painting blue")
word   <- c("green, red, blue", "red", "red, blue")
df     <- data.frame(country, text, word)

对于每一行,我想在文本列的文本中找到单词列中的单词,并将它们呈现在新列中,因此将在文本中显示已创建的单词,以逗号分隔。所以新列应该是:

df$new_col   <- c("green,red","red","blue")

我正在使用这些代码行,但运行甚至崩溃需要很多时间。

setDT(df)[, new_col:= paste(df$word[unlist(lapply(df$word,function(x) grepl(x, df$text,
     ignore.case = T)))], collapse = ","), by = 1:nrow(df)]

有没有办法更改代码以提高效率?

非常感谢!


共3个答案

匿名用户

试试这个

mapply(function(x,y){paste(intersect(x,y),collapse=", ")},
       strsplit(as.character(df$text),"\\, | "),
       strsplit(as.character(df$word),"\\, | "))

[1] "red, green" "red"        "blue"

匿名用户

另一个使用mapplicationgrep注册表的基本R解决方案,即,

df <- within(df, newcol <- mapply(function(x,y) toString(grep(x,y,value = TRUE)), 
                                  gsub("\\W+","|",word), 
                                  regmatches(text,gregexpr("\\w+",text))))

这样

> df
  country            text             word     newcol
1      CA paint red green green, red, blue red, green
2      IN    painting red              red        red
3      US   painting blue        red, blue       blue

匿名用户

library(tidyverse)    
df %>% 
   mutate(newcol = stringr::str_extract_all(text,gsub(", +","|",word)))
      country            text             word     newcol
    1      CA paint red green green, red, blue red, green
    2      IN    painting red              red        red
    3      US   painting blue        red, blue       blue

在这种情况下,newcol是一个列表。要使其成为字符串,我们可以这样做:

df%>%
  mutate(newcol = text %>%
           str_extract_all(gsub(", +", "|", word)) %>%
           invoke(toString, .))

使用data. table,您可以执行:

 df[,id := .I][,newcol := do.call(toString,str_extract_all(text,gsub(', +',"|",word))),
      by = id][, id := NULL][]
   country            text             word     newcol
1:      CA paint red green green, red, blue red, green
2:      IN    painting red              red        red
3:      US   painting blue        red, blue       blue