【发布时间】:2016-09-29 17:08:43
【问题描述】:
我想用特定的字符串分割数据框中的一些数据并计算频率。
玩弄了几种方法后,我想出了一个方法,但结果有一点错误。
例子:
数据框数据文件:
data
abc hello
hello
aaa
zxy
xyz
列表:
list
abc
bcd
efg
aaa
我的代码:
lapply(list$list, function(x){
t <- data.frame(words = stri_extract(df$data, coll=x))
t<- setDT(t)[, .( Count = .N), by = words]
t<-t[complete.cases(t$words)]
result<-rbind(result,t)
write.csv(result, "new.csv", row.names = F)
})
在此示例中,我希望 CSV 文件具有以下结果:
words Count
abc 1
aaa 1
但是我得到了我的代码:
words Count
aaa 1
我知道stri_extract 应该在abc hello 中识别abc,所以当我使用rbind 时可能会发生错误?
【问题讨论】:
-
另见:
stringi::stri_list2matrix