【发布时间】:2017-11-07 08:08:05
【问题描述】:
我有一个数据框,其中一列包含字符串。
q = data.frame(number=1:2,text=c("The surcingle hung in ribands from my body.", "But a glance will show the fallacy of this idea."))
我想对每条记录使用 word_stats 函数。 有可能吗?
【问题讨论】:
我有一个数据框,其中一列包含字符串。
q = data.frame(number=1:2,text=c("The surcingle hung in ribands from my body.", "But a glance will show the fallacy of this idea."))
我想对每条记录使用 word_stats 函数。 有可能吗?
【问题讨论】:
text_statistic <- apply(q,1,word_stats)
这将逐行应用word_stats() 并返回一个列表,其中包含每行word_stats() 的结果
【讨论】:
您可以通过多种方式做到这一点,lapply 或 sapply 在列表或向量上应用函数。
word_stats <- function(x) {length(unlist(strsplit(x, ' ')))}
sapply(q$text, word_stats)
【讨论】:
请看grouping.var 参数:
dat = data.frame(number=1:2,text=c("The surcingle hung in ribands from my body.", "But a glance will show the fallacy of this idea."))
with(dat, qdap::word_stats(text, number))
## number n.sent n.words n.char n.syl n.poly wps cps sps psps cpw spw pspw n.state p.state n.hapax grow.rate
## 1 2 1 10 38 14 2 10 38 14 2 3.800 1.400 .200 1 1 10 1
## 2 1 1 8 35 12 1 8 35 12 1 4.375 1.500 .125 1 1 8 1
【讨论】: