[R] counting across leves of factors

peterko lanikpeter at gmail.com
Wed Jun 9 21:13:13 CEST 2010


I have dataframe with 17factors variables (for example every factor have
3levels)
I have maybe 5000 observation.
And i need to do table where is in every raw 1 of possible combination of
this factors and the numbur how many time is this combination in my dataset.

I wrote one code, but this is very slow and dumb.

it looks like this:

i<-0

for(i1 in levels(hivdat$pohl)){ 
a<-subset(hivdat,hivdat$pohl==i1)
for(i2 in levels(a$vekF)){
b<-subset(a,a$vekF==i2)
for(i3 in levels(b$ekstF)){
c<-subset(b,b$ekst==i3)
for(i4 in levels(c$zam)){
d<-subset(c,c$zam==i4)
for(i5 in levels(d$saman)){
e<-subset(d,d$saman==i5)
for(i6 in levels(e$test)){
f<-subset(e,e$test==i6)
for(i7 in levels(f$aktiv)){
g<-subset(f,f$aktiv==i7)
for(i8 in levels(g$pocpartF)){
h<-subset(g,g$vekF==i8)
for(i9 in levels(h$stav)){
i<-subset(h,h$stav==i9)
for(i10 in levels(i$partner)){
j<-subset(i,i$partner==i10)
for(i11 in levels(i$aktiv6)){
k<-subset(j,j$aktiv6==i11)
for(i12 in levels(k$pocpart6F)){
l<-subset(k,k$pocpart6F==i12)
for(i13 in levels(l$styk6F)){
m<-subset(l,l$styk6F6==i13)
for(i14 in levels(k$kond6F)){
n<-subset(m,m$kond6F==i14)
for(i15 in levels(k$styd)){
o<-subset(n,n$styd==i15)
for(i16 in levels(k$zasl)){
p<-subset(o,o$zasl==i16)
for(i17 in levels(p$izol)){
r<-subset(p,p$izol==i17)    
i<-i+1
n<-length(r$pohl)
data2[i,]<-c(n,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17)
}}}}}}}}}}}}}}}}}

I believe that there exist smart solution, thanks
-- 
View this message in context: http://r.789695.n4.nabble.com/counting-across-leves-of-factors-tp2249355p2249355.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list