[R] adding column to data frame conditionally

Kristina Schmitz Krische at hsu-hh.de
Thu May 27 11:33:09 CEST 2010


Dear all and thanks in advance for helping me with a rather stupid question:
I imported a data set ("freqg") into R consisting of 14 variables. Now  
a want to compute a variable and add it in an additional column to my  
data frame. The value of this new variable ("condition") depends on  
the values of two other variables ("mat" and "flank") already included  
in the data frame.
For example:
if mat=1 and flank=1 -> condition=1
if mat=2 and flank=1 -> condition=2
...

What I got is this code, which doesn't really work (it results in a  
new column called "condition" which takes only the value "TRUE"):

freqg<-transform(freqg,condition=(mat=1)&(flank=1))

Thanks in advance and kind regards!



More information about the R-help mailing list