首先,我创建了一个假数据框...
# first a handy helper function :-)
set.seed(123)
f1 <- function(N) { sample(c('Yes','No','Unknown'), N, replace=TRUE) }
# how many rows do I want in my fake data.frame?
N <- 15
data <- data.frame(var1=f1(N),var2=f1(N),var3=f1(N),var4=f1(N),
level=sample(c('level1','level2'),N, replace=TRUE))
现在我使用表格功能...
# this gives me how many 'Yes' as the TRUE column for the 'var1' column
table(data$level, data$var1=='Yes')
FALSE TRUE
level1 5 2
level2 7 1
# alternatively I just say for all the levels of var1
table(data$level, data$var1)
No Unknown Yes
level1 3 2 2
level2 3 4 1
然后可以将其扩展到您要查找的内容...
或使用 plyr ...
library(plyr)
f2 <- function(x, c='Yes') { length(which(x==c)) }
f3 <- function(ndf) { res <- c(); for(i in 1:ncol(ndf)) res <- c(res, f2(ndf[,i])) ; res }
ddply(data, .(level), f3)
level V1 V2 V3 V4 V5
1 level1 2 5 2 3 0
2 level2 1 0 4 4 0