查找匹配的字符串列表

时间:2016-10-13 18:29:22

标签: r

我有推文中使用的主题标签数据集。每一行都是特定的推文,每个变量都是每个推文中使用的不同主题标签,因此对于某些观察,许多变量都是空的。因为他们的hasthags较少。我的最终目标是看到3个最流行的主题标签的共同出现,但为此我想首先看看哪些推文使用这些top3主题标签。

我的数据集看起来像这样:

    V1 |  V2  |  V3 |      top3
    nyc|      |     | nyc, cool, nyc2016
   cool| nyc  |     | nyc, cool, nyc2016
  hello| cool | nyc | nyc, cool, nyc2016
 winter| nyc  |     | nyc, cool, nyc2016

所以在这个例子中,前3个主题标签是nyc和cool,但不是你好和冬天。

我试着通过

查看每个标签是否在top3之中
    df1<-sapply(df$V1, function(x) grepl(sprintf('\\b%s\\b', x), df$top3))

但这花了太长时间。然后我必须为V2和V3做这个(可以做一个循环,但这需要更长的时间来运行)。

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

我们能否安全地假设top3在您的数据集中是唯一的?如果是这样的话:

df <- read.table(
  textConnection("    V1 |  V2  |  V3 |      top3
    nyc|      |     | nyc, cool, nyc2016
   cool| nyc  |     | nyc, cool, nyc2016
  hello| cool | nyc | nyc, cool, nyc2016
 winter| nyc  |     | nyc, cool, nyc2016"),
  sep = "|", header = TRUE, stringsAsFactors = FALSE, strip.white = TRUE)
library(dplyr) ; library(stringr)
top <- str_split(df$top3[[1]], pattern = ", ")[[1]]
is_in_top <- function(x) x %in% top
mutate_each(df, funs(is_in_top), vars = V1:V3)

答案 1 :(得分:1)

在进行此类操作之前,我总是尝试以规范化或长格式获取数据。我觉得我的数据更加灵活。虽然评论中提到的解决方案也可能有效,但我想分享我的解决方案:

library(dplyr)
library(tidyr)


df <- data.frame(v1 = c('nyc','cool','hello','winter')
                 ,v2 = c(NA,'nyc','cool','nyc')
                 ,v3 = c(NA,NA,'nyc',NA)
                 ,stringsAsFactors = F)
top3 <- c('nyc','cool','nyc2016')

df %>% mutate(id = row_number()) %>% gather(n, word,-id) %>% 
  filter(!is.na(word)) %>% group_by(id) %>%
  summarise(n_in_top3 = sum(ifelse(word %in% top3,1,0)))

结果:

id        n_in_top3
(int)     (dbl)
1         1
2         2
3         2
4         1

结果是一个摘要,其中包含数据中每行的前3个单词列表中有多少个单词。

如果您希望每个列都有TRUE/FALSE值,请执行以下操作:

df %>% mutate(id = row_number()) %>% gather(n, word,-id) %>% 
  filter(!is.na(word)) %>% group_by(id, n) %>%
  summarise(n_in_top3 = (word %in% top3)) %>%
  spread(n, n_in_top3)

给出:

id    v1      v2     v3
<int> <lgl>   <lgl>  <lgl>
1     TRUE    NA     NA
2     TRUE    TRUE   NA
3     FALSE   TRUE   TRUE
4     FALSE   TRUE   NA