假设我在R中有以下数据帧dat:
a <- c(6,6,6,7,9,10,11,13,16,17,19,20,22,10,23,25,32,32,34,35,6)
b <- c(1,1,0,1,0,1,0,1,1,0,0,0,1,0,1,0,0,0,0,0,1)
q <- c(3,2,4,2,4,2,2,3,0,5,0,6,1,0,3,4,9,1,2,0,12)
dat <- data.frame(a, b, q)
dat <- dat[order(dat$a),]
对于每个级别的a,我必须计算f = with(dat, sum(q[a==that level]