[R] Data manipulation question

Gabor Grothendieck ggrothendieck at gmail.com
Wed Oct 10 14:38:43 CEST 2007


Try this:

transform(d, z = y[match(x, id)])


On 10/10/07, Julien Barnier <jbarnier at ens-lsh.fr> wrote:
> 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
>
> ______________________________________________
> 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