我有一个由单词组成的列表。R计数次数字出现在列表元素中
> head(splitWords2)
[[1]]
[1] "Some" "additional" "information" "that" "we" "would" "need" "to" "replicate" "the"
[11] "experiment" "is" "how" "much" "vinegar" "should" "be" "placed" "in" "each"
[21] "identical" "container" "or" "what" "tool" "use" "measure" "mass" "of" "four"
[31] "different" "samples" "and" "distilled" "water" "rinse" "after" "taking" "them" "out"
[[2]]
[1] "After" "reading" "the" "expirement" "I" "realized" "that" "additional" "information" "you"
[11] "need" "to" "replicate" "expireiment" "is" "one" "amant" "of" "vinegar" "poured"
[21] "in" "each" "container" "two" "label" "containers" "before" "start" "yar" "and"
[31] "three" "write" "a" "conclusion" "make" "sure" "results" "are" "accurate"
我有一个单词向量,我想计算列表中每个元素的出现次数,而不是整个列表中出现的总次数。
我认为做到这一点的方法是使用stringr
包中的str_count()
函数和*ply()
函数之一的组合,但我无法使其工作。
numWorder1 <- sapply(ifelse(str_count(unlist(splitWords2), ignore.case("we"))> 0, 1, 0))
其中“我们”最终将成为单词矢量中的一个单词来计算出现次数。
我的理想输出会是这样的:
lineNum count
1 0
2 1
3 1
4 0
... ...
有什么建议?