[R] replacing NA for zero
    arun 
    smartpink111 at yahoo.com
       
    Wed Jun 13 03:02:30 CEST 2012
    
    
  
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.
    
    
More information about the R-help
mailing list