在 R 中使用 str_count() 函数时出错:'Input must be a vector, not an environment.'



我有一个数据帧df,它有一个chr列Col1,看起来像这样:

<表类> Col1 tbody><<tr>"苹果;橙色;pear""橙色apple"

一种基数R方法来计算以";"分隔的单词数是:

lengths(gregexpr(";", df$Col1)) + 1
# [1] 3 2

创建一个新列,只需:

df$NewCol <- lengths(gregexpr(";", df$Col1)) + 1
#                Col1 NewCol
# 1 apple;orange;pear      3
# 2      orange;apple      2

数据
df <- data.frame(Col1 = c("apple;orange;pear","orange;apple"))

相关内容

最新更新