如何计算R中的子组中出现值的实例数?
我在R中使用了一个数据帧,并试图检查一个值在其较大的关联组中出现了多少次。具体地说,我试图统计每个特定国家列出的城市数量 我的数据如下所示:如何计算R中的子组中出现值的实例数?,r,R,我在R中使用了一个数据帧,并试图检查一个值在其较大的关联组中出现了多少次。具体地说,我试图统计每个特定国家列出的城市数量 我的数据如下所示: City Country ========================= New York US San Francisco US Los Angeles US Paris France Nantes France Berlin
City Country
=========================
New York US
San Francisco US
Los Angeles US
Paris France
Nantes France
Berlin Germany
似乎table()是一个不错的选择,但我不太明白-我如何才能知道每个国家列出了多少个城市?也就是说,我如何找出一列中有多少字段与另一列中的特定值相关联
编辑:
我希望能有类似的东西
3 US
2 France
1 Germany
我想你可以试试
table
table(df$Country)
# France Germany US
# 2 1 3
或使用data.table
library(data.table)
setDT(df)[, .N, by=Country]
# Country N
#1: US 3
#2: France 2
#3: Germany 1
或
library(plyr)
count(df$Country)
# x freq
#1 France 2
#2 Germany 1
#3 US 3