如何计算列中未知字符串的出现? [英] How to count occurrence of unknown strings in column?
问题描述
我还有一个问题。感谢大家对R新手的帮助和耐心!
I have another question. Thanks for everyone's help and patience with an R newbie!
我如何计算一列中出现一个字符串的次数?例如:
How can I count how many times a string occurs in a column? Example:
MYdata <- data.frame(fruits = c("apples", "pears", "unknown_f", "unknown_f", "unknown_f"),
veggies = c("beans", "carrots", "carrots", "unknown_v", "unknown_v"),
sales = rnorm(5, 10000, 2500))
问题是我的真实数据集包含几千行和几百个未知数水果和不知名的蔬菜。我玩过 table()和 levels,但没有取得太大的成功。我想这要复杂得多。最好有一个输出表,列出每个独特水果/蔬菜的名称以及其列中出现的次数。朝正确方向的任何提示将不胜感激。
The problem is that my real data set contains several thousand rows and several hundred of the unknown fruits and unknown veggies. I played around with "table()" and "levels" but without much success. I guess it's more complicated than that. Great would be to have an output table listing the name of each unique fruit/veggie and how many times it occurs in its column. Any hint in the right direction would be much appreciated.
谢谢,
Marcus
推荐答案
如果我理解您的问题,则函数 table()
应该可以正常工作。方法如下:
If I understand your question, the function table()
should work just fine. Here is how:
table(MYdata$fruits)
apples pears unknown_f
1 1 3
table(MYdata$veggies)
beans carrots unknown_v
1 2 2
或在 lapply
表 >:
Or use table
inside lapply
:
lapply(MYdata[1:2], table)
$fruits
apples pears unknown_f
1 1 3
$veggies
beans carrots unknown_v
1 2 2
这篇关于如何计算列中未知字符串的出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!