我正在尝试从16077的推文中检索主题标签 例如,我想在下面的推文中列出所有主题标签
df<-c("Good #food to be had in #Sibu. Here are some leads #VisitSarawak #VMY2014", "Spore white card, well aren't you a sight for sore eyes! Thanks ! #finallygoinghome","Low cost air travel will drive intra-Asian integration; experiencing trend myself this week on and #fingerscrossed")
library(stringr)
hashtag<-str_extract_all(df, "#\\S+")
> hashtag
[[1]]
[1] "#food" "#Sibu." "#VisitSarawak" "#VMY2014"
[[2]]
[1] "#finallygoinghome"
[[3]]
[1] "#fingerscrossed"
如何将结果导出到csv文件中?我尝试使用下面的代码但导致错误
write.csv(hashtag,file='C:/R_working_directory/hashtag.csv',row.names=F)
Error in data.frame(character(0), character(0), character(0), character(0), :
arguments imply differing number of rows: 0, 1, 4, 2, 6, 3, 5, 7, 9, 8, 10, 11, 12, 13, 14
答案 0 :(得分:1)
您可以从列表中构建数据框,然后使用write.csv
:
dat <- data.frame(id=rep(seq(length(hashtag)), lapply(hashtag, length)),
hashtag=unlist(hashtag))
dat
# id hashtag
# 1 1 #food
# 2 1 #Sibu.
# 3 1 #VisitSarawak
# 4 1 #VMY2014
# 5 2 #finallygoinghome
# 6 3 #fingerscrossed
write.csv(dat, file='C:/R_working_directory/hashtag.csv',row.names=F)