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)) 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? Best wishes Troels Ring, Aalborg, Denmark
Hi Troels, Not sure this is what you want.> X<-runif(9,0,10) > FF1<-data.frame(ID=c(1,2,3)[rep(c(1,1,1,2,2,2,3,3,3))], PERIOD=c(1,2,3)[rep(c(1,2,3),times=3)],X=X) > FF1$X[4]<-NA > FF1? ID PERIOD????????? X 1? 1????? 1 8.27119347 2? 1????? 2 9.64698097 3? 1????? 3 2.74132386 4? 2????? 1???????? NA 5? 2????? 2 4.29322683 6? 2????? 3 5.09269667 7? 3????? 1 4.07936332 8? 3????? 2 7.41808455 9? 3????? 3 0.01558664 A.K. ----- Original Message ----- From: Troels Ring <tring at gvdnet.dk> To: r-help at r-project.org Cc: Sent: Wednesday, May 16, 2012 11:56 AM Subject: [R] simple data.frame question 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)) 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? 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.
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
Thanks a lot - beautiful Troels Den 16-05-2012 19:29, David Winsemius skrev:> > 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 > >