【问题标题】:R - Count the number of words in a text string variable [duplicate]R - 计算文本字符串变量中的单词数[重复]
【发布时间】:2020-08-17 06:31:12
【问题描述】:

我想统计我的数据中文本中的字数。

然后

我想使用循环来计算数据中每一行的特定列“意见”中的字数。

有什么建议吗?

【问题讨论】:

    标签: r dataframe for-loop while-loop


    【解决方案1】:

    我们可以从stringr使用str_count

    library(stringr)
    df1$nwords <- str_count(df1$Opinion, "\\w+")
    

    或者使用for 循环

    df1$nwords <- NA_integer_
    for(i in seq_along(df1$Opinion)) {
          df1$nwords[i] <- length(strsplit(df1$Opinion[i], "\\s+")[[1]])
     }
    

    或者在整列上加上strsplit

    df1$nwords <- lengths(strsplit(df1$Opinion, "\\s+"))
    

    【讨论】:

    • @JoeBens df1$nwords &lt;-
    猜你喜欢
    • 2014-06-04
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多