[R] Generating new variable based on values of an existing variable

Josip Dasovic jjd9 at sfu.ca
Mon Feb 9 20:30:14 CET 2009


Dear R Help-Listers:

I have a problem that seems like it should have a simple solution, but I've spent hours on it (and searching the r-help archives) to no avail. What I'd like to do is to generate a new variable within a data frame, the values of which are dependent upon the values of an existing variable within that data frame. 

Assume that I have the following data:

mydf<-data.frame(region=c(rep("North", 5), rep("East", 5), rep("South", 5), rep("West", 5)))

Assume, in addition, that I have a factor vector with four values (I actually have a factor with almost two-hundred values):

element<-c("earth", "water", "air", "fire")

I would like to add a new variable to the data frame (called "element") such that the value of "element" is "earth" in each observation for which mydf$region=="North", etc. In STATA, this was relatively easy; is there a simple way to do this in R? 

This is what the final result should look like:

> mydf
   region element
1   North   earth
2   North   earth
3   North   earth
4   North   earth
5   North   earth
6    East   water
7    East   water
8    East   water
9    East   water
10   East   water
11  South     air
12  South     air
13  South     air
14  South     air
15  South     air
16   West    fire
17   West    fire
18   West    fire
19   West    fire
20   West    fire

Thanks in advance,
Josip




More information about the R-help mailing list