【问题标题】:word_stats function from qdap package application on a dataframe数据帧上 qdap 包应用程序的 word_stats 函数
【发布时间】: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 函数。 有可能吗?

【问题讨论】:

    标签: r string dataframe qdap


    【解决方案1】:
    text_statistic <- apply(q,1,word_stats)
    

    这将逐行应用word_stats() 并返回一个列表,其中包含每行word_stats() 的结果

    【讨论】:

      【解决方案2】:

      您可以通过多种方式做到这一点,lapplysapply 在列表或向量上应用函数。

      word_stats <- function(x) {length(unlist(strsplit(x, ' ')))} sapply(q$text, word_stats)

      【讨论】:

        【解决方案3】:

        请看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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-05
          • 2020-04-13
          • 1970-01-01
          • 1970-01-01
          • 2020-03-29
          • 2015-02-13
          • 2021-03-09
          • 2014-10-03
          相关资源
          最近更新 更多