计算满足 R 中多个条件的行数

Count number of rows that fulfill multiple conditions in R

我有一个数据框:

df <- data.frame (ID  = c(1:20),
                  Ethnicity = c(rep(c("White", "Asian", "Black", "Hispanic", "Other"), times=20/5)),
                  Age = c(1:20),
                  Set = rep(c(1,2,3,4), times=20/4)
)

计算 Ethnicity== 'Asian' 和 Set == 3 的行数的最有效方法是什么?

谢谢!

取决于您衡量效率的标准,但是

sum(df$Ethnicity== 'Asian' & df$Set == 3)