计算条件为2.0时的观测值总和

时间:2020-09-29 15:46:24

标签: r

给定的数据是:

farmer<-c("F1","F2","F3","F4","F5","F6")
animal<-c("A1","A2","A3","A4","A5","A6","A7","A8")
time<-c("T1","T2","T3","T4","T5")
disease<-c("ww","ee","dd","ss","kk","ll","xx","oo","hh")
sample(farmer,size=100,replace = TRUE)

df<-data.frame(farmer=sample(farmer,size=100,replace = TRUE),
               animal=sample(animal,size=100,replace = TRUE),
               time=sample(time,size=100,replace = TRUE),
               disease=sample(disease,size=100,replace = TRUE))
head(df)

enter image description here

问题:在特定时间和特定动物患有相同疾病的农民人数?

输出应如下所示:

enter image description here

1 个答案:

答案 0 :(得分:0)

library(data.table)

dt <- setDT(df)

dt[, .(NumberOfFarmers = .N), by = .(animal, time, disease)]

我想这应该是您要寻找的东西。