Dear R users, I have a very basic query, but was unable to find a proper anwser. I have the following data.frame x y 2 0.12 3 0.25 4 0.11 6 0.16 7 0.20 and, due to further calculations, I need the data to be stored as x y 1 0 2 0.12 3 0.25 4 0.11 5 0 6 0.16 7 0.20 8 0 How do I do the transformation? Many many thjanks in advance.
Hi, Try this: ?dat1<-data.frame(x=1:8,y=c(NA,0.12,0.25,0.11,NA,0.16,0.20,NA)) ?dat1[is.na(dat1)]<-0 ?dat1 ? x??? y 1 1 0.00 2 2 0.12 3 3 0.25 4 4 0.11 5 5 0.00 6 6 0.16 7 7 0.20 8 8 0.00 A.K. ----- Original Message ----- From: Silvia Lucato <slucato at lycos.com> To: r-help at r-project.org Cc: Sent: Tuesday, June 12, 2012 5:55 PM Subject: [R] replacing NA for zero ? Dear R users, ? I have a very basic query, but was unable to find a proper anwser. ? I have the following data.frame ? x? ? y ? 2? 0.12 ? 3? 0.25 ? 4? 0.11 ? 6? 0.16 ? 7? 0.20 ? and, due to further calculations, I need the data to be stored as ? x? ? y ? 1? ? 0 ? 2? 0.12 ? 3? 0.25 ? 4? 0.11 ? 5? ? 0 ? 6? 0.16 ? 7? 0.20 ? 8? ? 0 ? How do I do the transformation? ? Many many thjanks in advance. ______________________________________________ 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.
> Dear R users, > > I have a very basic query, but was unable to find a proper anwser. > > I have the following data.frame > > x y > 2 0.12 > 3 0.25 > 4 0.11 > 6 0.16 > 7 0.20 > > and, due to further calculations, I need the data to be stored as > > x y > 1 0 > 2 0.12 > 3 0.25 > 4 0.11 > 5 0 > 6 0.16 > 7 0.20 > 8 0 > > How do I do the transformation?Let's suppose your original data frame is DF1. Then: DF2 <- data.frame(x=1:8,y=rep(0,8)) DF2$y[DF1$x] <- DF1$y But I'd recommend you use NA for the missing values, unless you have a good reason to code them as zero.> Many many thjanks in advance.You are welcome. Helios De Rosario -- Helios de Rosario Mart?nez Researcher INSTITUTO DE BIOMEC?NICA DE VALENCIA Universidad Polit?cnica de Valencia ? Edificio 9C Camino de Vera s/n ? 46022 VALENCIA (ESPA?A) Tel. +34 96 387 91 60 ? Fax +34 96 387 91 69 www.ibv.org Antes de imprimir este e-mail piense bien si es necesario hacerlo. En cumplimiento de la Ley Org?nica 15/1999 reguladora de la Protecci?n de Datos de Car?cter Personal, le informamos de que el presente mensaje contiene informaci?n confidencial, siendo para uso exclusivo del destinatario arriba indicado. En caso de no ser usted el destinatario del mismo le informamos que su recepci?n no le autoriza a su divulgaci?n o reproducci?n por cualquier medio, debiendo destruirlo de inmediato, rog?ndole lo notifique al remitente.
A simplistic approach which works for the given example is as follows: Let your data frame be "X". Do: x <- 1:8 y <- numeric(8) y[X$x] <- X$y Y <- data.frame(x=x,y=y) Then "Y" is the desired result. cheers, Rolf Turner On 13/06/12 09:55, Silvia Lucato wrote:> Dear R users, > > > > I have a very basic query, but was unable to find a proper anwser. > > > > I have the following data.frame > > > > x y > > 2 0.12 > > 3 0.25 > > 4 0.11 > > 6 0.16 > 7 0.20 > > > > and, due to further calculations, I need the data to be stored as > > > > x y > > 1 0 > > 2 0.12 > > 3 0.25 > > 4 0.11 > > 5 0 > > 6 0.16 > 7 0.20 > > 8 0 > > > > How do I do the transformation? > > > > Many many thjanks in advance. > ______________________________________________ > 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. >
Hi, I got confused by your heading when I sent the previous reply.? There is another way to reach the same conclusion. Try this: DF1<-data.frame(x=c(2,3,4,6,7),y=c(0.12,0.25,0.11,0.16,0.20)) DF2<-data.frame(x=1:8,y=rep(NA,8)) ?df3<-merge(DF1,DF2,all=TRUE) ?df3[is.na(df3)]<-0 g<-rle(df3[,1]) gVa<-g$value gLe<-g$lengths idg<-c(0,head(cumsum(gLe),-1))+1 df4<-data.frame(x=gVa,y=df3[idg,2])> df4? x??? y 1 1 0.00 2 2 0.12 3 3 0.25 4 4 0.11 5 5 0.00 6 6 0.16 7 7 0.20 8 8 0.00 A.K. ----- Original Message ----- From: Silvia Lucato <slucato at lycos.com> To: r-help at r-project.org Cc: Sent: Tuesday, June 12, 2012 5:55 PM Subject: [R] replacing NA for zero ? Dear R users, ? I have a very basic query, but was unable to find a proper anwser. ? I have the following data.frame ? x? ? y ? 2? 0.12 ? 3? 0.25 ? 4? 0.11 ? 6? 0.16 ? 7? 0.20 ? and, due to further calculations, I need the data to be stored as ? x? ? y ? 1? ? 0 ? 2? 0.12 ? 3? 0.25 ? 4? 0.11 ? 5? ? 0 ? 6? 0.16 ? 7? 0.20 ? 8? ? 0 ? How do I do the transformation? ? Many many thjanks in advance. ______________________________________________ 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.