[R] Counting observations of a combined factor

Sam Player samtplayer at gmail.com
Sat Sep 19 11:39:18 CEST 2009


#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?

-- 
Sam Player, B.Sc.(Hons.) B.A.
Ph.D. Candidate, Faculty of Agriculture, Food & Natural Resources, University of Sydney

Email: splayer at usyd.edu.au

Agroecosystems Research Group
Room 214 J.R.A. McMillan Building A05
University of Sydney NSW 2006, Australia

Angkor Research Program
Room 305 Old Teachers College A22
University of Sydney NSW 2006, Australia




More information about the R-help mailing list