Tabulate two variables by a third one

#1
Hello,
I'm trying to avoid a loop to do this
Code:
zz=matrix(NA, nrow=40,ncol=1)

for (i in 1:40) {

wer=table(id[qwe==i],strata[qwe==i])
df=nrow(wer)-ncol(wer)

zz[i]=df

}
The variable qwe goes from 1 to 40, and I need the nrow-ncol from a crossing two variables (id, strata)
Is It possible to adapt this code using a tapply or similar function?
I'm interested because I don't want to programa like I do in stata using R. I think that's not the point of using R.
Thanks for your time and interest.[/CODE]