[R] duplicating records
Jim Lemon
jim at bitwrit.com.au
Sat Feb 6 07:31:41 CET 2010
On 02/06/2010 12:53 PM, El-Tahtawy, Ahmed wrote:
> Dear friends,
>
>
>
> I need to fill in (duplicate the whole record) the missing days with the
> same record values as long as AE is the same value (i.e. "1"), once AE
> value changes, the process of duplication should proceed with the new AE
> value till it changes again. e.g. I need to fill in records: day 18-day
> 44, all the records are carried with the new AE value of "0".
>
>
>
> At the end for patient ID "1" we will end up with 73 records (for 73
> days).
Hi Ahmed,
This hasn't been extensively tested, but it might do what you want.
fillMissingDays<-function(x) {
xdim<-dim(x)
lastday<-max(x$DAY)
newx<-as.data.frame(matrix(NA,nrow=xdim[1],ncol=xdim[2]))
names(newx)<-names(x)
newx[1,]<-x[1,]
startrow<-2
for(xrow in 2:xdim[1]) {
endrow<-x$DAY[xrow]
newday<-x$DAY[xrow-1]+1
for(newrow in startrow:endrow) {
newx[newrow,]<-x[xrow-1,]
newx$DAY[newrow]<-newday
newday<-newday+1
}
startrow<-endrow+1
}
return(newx)
}
Call as fillMissingDays(<your data frame>)
Jim
More information about the R-help
mailing list