用R从regmatches创建数据框

3
我已经搜寻了很久,但是无法理解如何将regmatches的输出转换成任何可以导出的内容。希望这个问题不是太具体,对社区没有价值。我有一个类似于以下链接中的问题:Extracting hashtags in several tweets using R
然而,我无法弄清楚如何从regmatches生成的列表中保存/导出/制作数据框。理想情况下,每个hashtag都会保存在单独的列中,但是每次尝试时我都会得到像这样的输出结果:
[[6267]]
character(0)

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

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

如果我尝试导出regmatches的结果,我会得到:

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")

从这些数据中,我想要提取出所有的hashtag(#)和紧随其后的单词,并将它们分配到不同的列中。上面链接中的代码已经完成了第一部分。

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

你想让数据框长成什么样子?你需要每个hashtag都有一列,每条推文都有一行吗? - C8H10N4O2
1
使用dput命令将一些数据导出,以便创建一个小的数据集和可重现该错误的代码示例。 - SabDeM
也许可以尝试使用data.table包(1.9.5+)中的rbindlist(df,fill=T)函数,它至少会给你一个data.frame,尽管可能是以非常混乱的形式呈现。 - MichaelChirico
2个回答

3
如果您有大量的推文和唯一的哈希标签,建议考虑使用稀疏矩阵。您可以在arules包中找到一个这样的稀疏矩阵对象itemMatrix。您可以将列表直接转换为此稀疏矩阵,而无需编写@LegalizeIt答案中的uniquesapply步骤(这是一个很好的基本解决方案,我会点赞)。
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

你可能还想考虑让你的标签不区分大小写,这样 #london2012 和 #London2012 就会被归为一组。也许可以使用 ms <- tolower(regmatches(...)) - C8H10N4O2

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

这些行对应于推文。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接