【发布时间】:2022-10-23 18:38:39
【问题描述】:
我有以下数据框:
structure(list(g = c("1", "2", "3"), x = c("This is text.", "This is text too.",
"This is no text"), y = c("What is text?", "Can it eat text?",
"Maybe I will try.")), class = "data.frame", row.names = c(NA,
-3L))
我想计算x 和y 列中的单词数,并将该值相加得到一列,其中包含每列使用的总单词数。重要的是我能够对数据进行子集化。结果应如下所示:
structure(list(g = c("1", "2", "3"), x = c("This is text.", "This is text too.",
"This is no text"), y = c("What is text?", "Can it eat text?",
"Maybe I will try."), z = c("6", "8", "8")), class = "data.frame", row.names = c(NA,
-3L))
我尝试将str_count(" ") 与不同的正则表达式结合使用across 或apply,但我似乎没有得到解决方案。
在我最初的问题中,我没有预料到其中包含NA 单元格的列会出现问题,但我确实做到了。因此,任何解决方案都需要能够处理NA 单元格。
【问题讨论】:
标签: r