regexed我有一个包含鸣叫列表的数据表中使用Twitter的库抓取并希望得到与重塑基于从单个列
因此,例如注释鸣叫的列表中选择多个项和其他行的data.frame ,我开始:
tmp=data.frame(tweets=c("this tweet with #onehashtag","#two hashtags #here","no hashtags"),dummy=c('random','other','column'))
> tmp
tweets dummy
1 this tweet with #onehashtag random
2 #two hashtags #here other
3 no hashtags column
,并希望产生:
result=data.frame(tweets=c("this tweet with #onehashtag","#two hashtags #here","#two hashtags #here","no hashtags"),dummy=c('random','other','other','column'),tag=c('#onehashtag','#two','#here',NA))
> result
tweets dummy tag
1 this tweet with #onehashtag random #onehashtag
2 #two hashtags #here other #two
3 #two hashtags #here other #here
4 no hashtags column <NA>
我可以使用正则表达式:
library(stringr)
str_extract_all("#two hashtags #here","#[a-zA-Z0-9]+")
来提取鸣叫标签到一个列表,可能使用类似:
tmp$tags=sapply(tmp$tweets,function(x) str_extract_all(x,'#[a-zA-Z0-9]+'))
> tmp
tweets dummy tags
1 this tweet with #onehashtag random #onehashtag
2 #two hashtags #here other #two, #here
3 no hashtags column
但我缺少某处一招并不能看到如何使用这个作为基础创建重复的行...