[R] Data manipulation question

Julien Barnier jbarnier at ens-lsh.fr
Wed Oct 10 12:10:29 CEST 2007


Hi all,

Suppose I have the following data.frame, with an id column and two
variables columns :

id        X       Y
0001      NA      21
0002      NA      13
0003      0001    45
0004      NA      71
0005      0003    20

What I would like to do is to create a new variable Z whose values are
the Y value for the id value in X, that is :

id        X       Y          Z
0001      NA      21         NA
0002      NA      13         NA
0003      0001    45         21
0004      NA      71         NA
0005      0003    20         45

Do you have an idea on how to obtain that without using a for loop ?

Thanks in advance for any help,

Julien



Here is the R code to reproduce the first data.frame :

id <- c("0001","0002","0003","0004","0005")
x <- c(NA, NA, "0001", NA, "0003")
y <- c(21,13,45,71,20)
d <- data.frame(id,x,y)



-- 
Julien Barnier
Groupe de recherche sur la socialisation
ENS-LSH - Lyon, France



More information about the R-help mailing list