从R中的regmatches创建数据框

时间:2015-07-06 21:29:48

标签: regex r

我已经猎杀并且无法理解如何将regmatches的输出转换为可以导出的任何内容。希望这个问题不是那么具体,对社区来说毫无价值。我遇到了与以下链接中的问题类似的问题:

Extracting hashtags in several tweets using R

但是,我无法弄清楚如何从regmatches列表中保存/导出/创建数据框。理想情况下,每个标签都会保存在一个单独的列中。但是每次尝试我都会得到一个输出:

[[6267]]
character(0)

[[6268]]
[1] "#ASCO15"

[[6269]]
[1] "#FDA"        "#Fast"       "#Track"      "#AML"        "#Pancreatic"    

如果我尝试导出我得到的调度结果:

Error in data.frame(character(0), character(0), character(0), character(0),  : 
  arguments imply differing number of rows: 0, 8, 2, 3, 5, 1, 4, 7, 6, 9 

谢谢你

编辑: 对不起,我本可以做一个糟糕的工作来解释自己。

dput(hi)
structure(list(text = c("Hooray ! #Wimbledon2Day has plugged its brain back in at last ! No more sub- Top Gear telly #propertenniscoverage", 
"gone but never forgotten #TopGear ", "The final episode of 'Top Gear' with Jeremy Clarkson is going to break records http://brbr.co/1JCeJYc\312"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-3L), .Names = "text")

根据该数据,我想提取主题标签(#)及其后的单词,并将它们分配给列。上面链接中的代码执行了第一部分。

test<-regmatches(hi$text,gregexpr("#(\\d|\\w)+",hi$text),)

给我:

[[1]]
[1] "#Wimbledon2Day"        "#propertenniscoverage"

[[2]]
[1] "#TopGear"

[[3]]
character(0)

但是当我尝试检查或导出它时,我得到:

Error in data.frame(c("#Wimbledon2Day", "#propertenniscoverage"), "#TopGear",  : 
  arguments imply differing number of rows: 2, 1, 0

2 个答案:

答案 0 :(得分:2)

如果你有大量的推文和独特的标签,你应该考虑使用稀疏矩阵。您可以在itemMatrix包中找到一个这样的稀疏矩阵对象arules。您可以直接将列表强制转换为此稀疏矩阵,而无需在@ LegalizeIt的答案中写出uniquesapply步骤(这是一个很好的基础解决方案,我给他+1)。

foo <- c("RddzAlejandra: RT @NiallOfficial: What a day for @johnJoeNevin ! Sooo proud t have been there to see him at #London2012 and here in mgar #MullingarShuffle","BPOInsight: RT @atos: Atos completes delivery of key IT systems for London 2012 Olympic Games http://t.co/Modkyo2R #london2012","BloombergWest: The #Olympics sets a ratings record for #NBC, with 219M viewers tuning in. http://t.co/scGzIXBp #london2012 #tech")

ms <- regmatches(foo, gregexpr("#(\\d|\\w)+", foo))  # extract hashtags from tweet (from other post)

library(arules)
im <- as(ms, "itemMatrix")

#you can retrieve the rows like this
as(im,"matrix")
#   #london2012 #London2012 #MullingarShuffle #NBC #Olympics #tech
# 1           0           1                 1    0         0     0
# 2           1           0                 0    0         0     0
# 3           1           0                 0    1         1     1

答案 1 :(得分:1)

使用链接帖子中的示例

foo <- c("RddzAlejandra: RT @NiallOfficial: What a day for @johnJoeNevin ! Sooo proud t have been there to see him at #London2012 and here in mgar #MullingarShuffle","BPOInsight: RT @atos: Atos completes delivery of key IT systems for London 2012 Olympic Games http://t.co/Modkyo2R #london2012","BloombergWest: The #Olympics sets a ratings record for #NBC, with 219M viewers tuning in. http://t.co/scGzIXBp #london2012 #tech")

ms <- regmatches(foo, gregexpr("#(\\d|\\w)+", foo))  # extract hashtags from tweet (from other post)
cols <- unique(unlist(ms))                           # get unique hashtags

setNames(data.frame(t(sapply(ms, function(i) cols %in% i))), cols)

#   #London2012 #MullingarShuffle #london2012 #Olympics  #NBC #tech
# 1        TRUE              TRUE       FALSE     FALSE FALSE FALSE
# 2       FALSE             FALSE        TRUE     FALSE FALSE FALSE
# 3       FALSE             FALSE        TRUE      TRUE  TRUE  TRUE

这些行对应于推文。