[R] Odp: Data manipulation question

Petr PIKAL petr.pikal at precheza.cz
Wed Oct 10 13:07:08 CEST 2007


Hi

r-help-bounces at r-project.org napsal dne 10.10.2007 12:10:29:

> 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 ?

d$z<-NA
d$z[d$x %in% d$id] <- d$y[d$id %in% d$x]

works in this particular case but it means you do not have multiple same 
ids and X

Regards
Petr

> 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list