我提供的数据样本如下:
Species Individuals
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
A 1
B 2
C 3
C 1
B 2
B 3
A 1
我期待输出为:
Species Frequency
A 8
B 13
C 10
是否可以使用R?
执行此操作答案 0 :(得分:2)
尝试
aggregate(cbind(Frequency=Individuals)~Species,df, sum)
# Species Frequency
#1 A 8
#2 B 13
#3 C 10
或者
library(data.table)
setDT(df)[, list(Frequency=sum(Individuals)), by=Species]
# Species Frequency
#1: A 8
#2: B 13
#3: C 10
答案 1 :(得分:1)
您可能会发现dplyr
赏心悦目的直观流程。增加的优点是结果仍然是数据框。
library(dplyr)
your_data %>% group_by(Species) %>% summarise(Frequency=sum(Individuals))
# Species Individuals
# 1 A 8
# 2 B 13
# 3 C 10