To get the counts, assuming your data frame is called factors and it only
contains the 17 factors, you can do
n = nrow(factors)
aux = rep(1, n);
tab = tapply(aux, as.list(factors), sum);
example:
factors = matrix(sample(c(1:3), 3000, replace = TRUE), 1000, 3)
lfactors = as.list(data.fran = nrow(factors)
n = nrow(factors)
aux = rep(1, n);
tab = tapply(aux, lfactors, sum);
Result:>tab
, , X3 = 1
X2
X1 1 2 3
1 32 38 45
2 39 43 46
3 47 36 24
, , X3 = 2
X2
X1 1 2 3
1 37 36 32
2 40 36 33
3 39 42 39
, , X3 = 3
X2
X1 1 2 3
1 40 47 31
2 36 30 33
3 34 37 28
However, I don't have a quick way of flattening the table and getting the
corresponding levels of each factor. Also note that with 17 factors and 3
levels in each, the table will have 3^17 = 129140163 (thats 129 million)
rows. If the table has 18 columns (17 values of factors + counts), the table
will occupy at least 18 gigabytes on your computer. If your computer doesn't
have that much memory, don't even bother trying.
On Wed, Jun 9, 2010 at 12:13 PM, peterko <lanikpeter@gmail.com> wrote:
>
> 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.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]