[R] Odp: Data manipulation question

Petr PIKAL petr.pikal at precheza.cz
Wed Oct 10 15:03:28 CEST 2007


r-help-bounces at r-project.org napsal dne 10.10.2007 13:55:32:

> Hi Petr,
> 
> > 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
> 
> Thanks for the idea. But the problem is that I can have multiple
> ids...
> 
> In fact in the meantime I found a solution by using row names :

are you sure?

> 
> R> d
>     id    x  y
> 1 0001 <NA> 21
> 2 0002 <NA> 13
> 3 0003 0001 45
> 4 0004 <NA> 71
> 5 0005 0003 20
> 
> R> rownames(d) <- d$id
> R> d$z <- NA
> R> d$z <- d[d$x,"y"]
> R> d
>        id    x  y  z
> 0001 0001 <NA> 21 NA
> 0002 0002 <NA> 13 NA
> 0003 0003 0001 45 21
> 0004 0004 <NA> 71 NA
> 0005 0005 0003 20 13

Why 13 in row 5. And using your code my result is

> d
    id    x  y
1 0001 <NA> 21
2 0002 <NA> 13
3 0003 0001 45
4 0004 <NA> 71
5 0005 0003 20
> d$z <- NA
> rownames(d) <- d$id
> d$z <- d[d$x,"y"]
> d
       id    x  y  z
0001 0001 <NA> 21 21
0002 0002 <NA> 13 21
0003 0003 0001 45 13
0004 0004 <NA> 71 21
0005 0005 0003 20 45

Regards
Petr

> 
> 
> Thanks for your help,
> 
> Julien
> 
> -- 
> 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