[R] Counting observations of a combined factor

Peter Ehlers ehlers at ucalgary.ca
Sat Sep 19 16:27:31 CEST 2009


Sam,

Depending on what your ultimate aim is, perhaps you just want to add
the 'drop=TRUE' argument to your interaction call.

Peter

Sam Player wrote:
> #I have a dataset with two factor. I want to combine those factors into 
> a single factor and count the number of data values for each new factor. 
> The following gives a comparable dataframe:
> 
> a <- rep(c("a", "b"), c(6,6))
> b <- rep(c("c", "d"), c(6,6))
> df <- data.frame(f1=a, f2=b, d=rnorm(12))
> df
> 
> # I use the 'interaction' function to combine factors f1 and f2:
> 
> df2 <- data.frame(f3=interaction(df[,"f1"], df[,"f2"]), d=df[,"d"])
> df2
> 
> # A count of the first data.frame using factor f1 returns the kind of 
> results I am looking for:
> 
> count <- as.data.frame(table(df$f1))
> count
> 
> #  Var1 Freq
> #1    a    6
> #2    b    6
> 
> # As does a count using factor2:
> 
> count2 <- as.data.frame(table(df$f2))
> count2
> 
> #  Var1 Freq
> #1    a    6
> #2    b    6
> 
> # The same procedure on the second dataframe does not treat the levels 
> of factor f3 discreetly, instead giving all possible combinations of f1 
> and f2.
> 
> count3 <- as.data.frame(table(df2$f3))
> count3
> 
> #  Var1 Freq
> #1  a.c    6
> #2  b.c    0
> #3  a.d    0
> #4  b.d    6
> 
> I need the results to be:
> 
> #  Var1 Freq
> #1    a    6
> #2    b    6
> 
> # Any suggestions?
>




More information about the R-help mailing list