【发布时间】:2014-07-08 18:03:22
【问题描述】:
请问有人可以帮我解答这个问题吗?非常感谢!
我有一些这样的数据:
A B
fruit red apple
fruit green apple
fruit yellow apple
fruit kiwi
fruit golden kiwi
juice apple juice
juice orange juice
我想得到以下信息:
A B freq
fruit apple 3
fruit kiwi 2
juice apple 1
juice orange 1
我可以提供要在 B 中搜索的字符串向量(即我知道我想查找“apple”、“kiwi”和“orange”)。例如,如果“fruit”中有“banana”,而我要搜索的项目列表中没有“banana”,则只需在结果中显示“banana”,频率为 1。
【问题讨论】:
-
尝试
sum(grepl('apple',data[,2]))获取带有 "apple" 的行数。 -
@CarlWitthoft,谢谢!但我真正想要的是A中每个类别中“苹果”的频率,即“水果”和“果汁”中“苹果”的频率。
-
好吧,然后想想 :-)
sum(grepl('apple',data[data[,1]=="fruit",2]))等等。 -
你是对的,这确实有效!但问题是,我的数据集非常大,A 中的类别(数千个)比给定的两个多得多。有没有更自动化的方法?
标签: r