[R] adding a factor column based on levels of another factor

From: Karen Kotschy <karen_at_biology.biol.wits.ac.za>
Date: Tue 12 Jul 2005 - 20:50:33 EST

Hi R users

Does anyone out there have a better/quicker way of adding a factor column to a data frame based on levels of another factor?

I have a (large) data frame consisting of records for individual plants, each represented by a unique ID number. The species of each plant is indicated in the column "species", which is a factor column with many different levels (species). There are multiple records for each species, and there is no pattern to the order in which the species names appear in the data frame.


   uniqueID species elev ht diam

1  1        sp2       3.5    1.3  55
2  2        sp2       4.2    0.5  15
3  3        sp3       3.2    1.0  13
4  4        sp65      2.2    2.0  14
5  5        sp43      5.4    5.7  20
6  6        sp2       2.5    4.1  32
7  7        sp12      1.1    0.9  5
8  8        sp3       3.4    3.6  2

I would like to add a factor column to this data frame, indicating to which group each individual belongs. All individuals of the same species will belong to the same group.

Is there a quick way of saying "for all instances of species1, give the value 5, for all instances of species2, give the value 4, etc" (where 5 and 4 are levels of a factor)?

The only way I can think of doing it is to split the data frame by species, then add a column to each subset showing the group, then re-join all the subsets. This seems clumsy and prone to errors. Anyone know a better way?

I've looked at expand.grid and gl but they don't seem to do what I want.


Karen Kotschy
Centre for Water in the Environment
University of the Witwatersrand
South Africa

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 12 20:55:01 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:32 EST