[R] simple data.frame question

David Winsemius dwinsemius at comcast.net
Wed May 16 19:29:18 CEST 2012


On May 16, 2012, at 11:56 AM, Troels Ring wrote:

> Dear friends - I hope you will forgive me another simple question,  
> illustrated by
>
> ID <- c(1,1,1,2,2,3,3,3)
> PERIOD <- c(1,2,3,2,3,1,2,3)
> X <- runif(8,0,10))

Extraneous paren removed:

>
> FF <- data.frame(ID=ID,PERIOD=PERIOD,X=X)
>
> I need to the fourth value of X as NA, and ID and PERIOD is updated  
> to 1,1,1,2,2,2,3,3,3 and 1,2,3,1,2,3,1,2,3 respectively.
> How do I use the pattern in ID and PERIOD to find the lacking X and  
> put NA?

 > ffnew=merge(x=expand.grid(1:3,1:3),
+ y=FF, by =1:2, all.x=TRUE)
 > ffnew
   Var1 Var2         X
1    1    1 6.6294571
2    1    2 0.5749111
3    1    3 8.7895630
4    2    1        NA
5    2    2 5.7213062
6    2    3 6.1030507
7    3    1 8.9182841
8    3    2 4.2823937
9    3    3 8.8249263
>
> Best wishes
>
> Troels Ring,
> Aalborg, Denmark
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list