【问题标题】:How do you count words in a df column and save it so that it matches the word count with ID?您如何计算 df 列中的单词并将其保存以使其与 ID 的单词计数相匹配?
【发布时间】:2020-11-25 11:50:04
【问题描述】:

这可能是一个非常简单的问题,但在查看以前的线程后我无法弄清楚。

我有一个包含两列的 df:ID 和 Response,Response 变量是一个字符串,其中输入的多个单词用空格分隔。

我先用空格分隔单词:

words <- str_split(d_words$Response, " ")

#Make sure everything is split properly:

words2 <- gsub("\n", " ", words)

#Split again:

words3 <- str_split(words2, " ")

#And then count:

wordcount <-lapply(words3, length)

这为我提供了每行的正确字数,但我似乎无法将原始 d_words df 与字数合并。如何检索或维护 ID 变量?

谢谢。

【问题讨论】:

    标签: r merge word-count


    【解决方案1】:

    既然您没有更改顺序,为什么不直接cbind(d_words, wordcount)

    【讨论】:

      猜你喜欢
      • 2013-12-25
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      相关资源
      最近更新 更多