r - How can i do tapply with filter on one of the variables -


i'm using tapply function in order count of variable on variable. here line of code: tapply(vip$var1,vip$var2,length)

however, filter observations have value "1" on vip$var1, can tapply?

dat <- read.table(text = " var1 var2 admit       num  0        0        0         7  0        0        1         1  0        1        0         3  0        1        1         7  1        0        0         5  1        0        1         1  1        1        0         0  1        1        1         6", header = true) 

would trick?

tapply(dat[dat$var1==1,]$var1,dat[dat$var1==1,]$var2,length) 

or simpler (from ananda mahto's comment):

with(dat[dat$var1 == 1, ], tapply(var1, var2, length)) 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -