Skip to content
Prev 309782 / 398506 Next

Replacing NAs in long format

HI Bill,
It is much simpler.
# with aggregate() and merge()? 

res1<-with(dat2,aggregate(seq_len(nrow(dat2)),by=list(idr=idr),FUN=function(i) with(dat2[i,], any(schyear<=5 & year ==0))))
?res2<-merge(dat2,res1,by="idr")
?colnames(res2)[4]<-"flag"
?within(res2,{flag<-as.integer(flag)})
?#idr schyear year flag
#1?? 1?????? 4?? -1??? 1
#2?? 1?????? 5??? 0??? 1
#3?? 1?????? 6??? 1??? 1
#4?? 1?????? 7??? 2??? 1
#5?? 2?????? 9??? 0??? 0
#6?? 2????? 10??? 1??? 0
#7?? 2????? 11??? 2??? 0


A.K.






----- Original Message -----
From: William Dunlap <wdunlap at tibco.com>
To: arun <smartpink111 at yahoo.com>; Christopher Desjardins <cddesjardins at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Saturday, November 3, 2012 9:21 PM
Subject: RE: [R] Replacing NAs in long format

Or, even simpler,
? idr schyear year? flag
1?  1? ? ?  4?  -1? TRUE
2?  1? ? ?  5? ? 0? TRUE
3?  1? ? ?  6? ? 1? TRUE
4?  1? ? ?  7? ? 2? TRUE
5?  2? ? ?  9? ? 0 FALSE
6?  2? ? ? 10? ? 1 FALSE
7?  2? ? ? 11? ? 2 FALSE

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com