[R] Aggregate Values for All Levels of a Factor

Kaom Te te.kaom at gmail.com
Fri Oct 6 00:44:28 CEST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I'm a novice user trying to figure out how to retain NA aggregate
values. For example, given a data frame with data for 3 of the 4
possible factor colors("orange" is omitted from the data frame), I want
to calculate the average height by color, but I'd like to retain the
knowledge that "orange" is a possible factor, its just missing. Here is
the example code:

> data <- data.frame(color = factor(c("blue","red","red","green","blue"),
        levels = c("blue","red","green","orange")),
	height = c(2,8,4,4,5))
> aggregate(data$height, list(color = data$color), mean)
  color   x
1  blue 3.5
2   red 6.0
3 green 4.0
>

Instead I would like to get

   color   x
1   blue 3.5
2    red 6.0
3  green 4.0
4 orange  NA

Is this possible. I've read as much documentation as I can find, but am
unable to find the solution. It seems like something people would need
to do. So I would assume it must be built in somewhere or do I need to
write my own version of aggregate?

Thanks in advance,
Kaom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFJYrLaaZgZdCbWv4RApNoAJ9jqKXne3IlQnd+PprS+7Kz1l4oRACfeu5I
Nv/xYWVsSGJD5+fdCP+02jk=
=b5TI
-----END PGP SIGNATURE-----



More information about the R-help mailing list