Hi, Please ?dput() your example dataset. Not sure this helps or not. u3s<- read.table(text="Current-ID visit AUC Wight ID1 101 3 . . 1 101 4 10 13 2 101 5 . . 3 102 3 . . 4 102 4 4 10 5 102 5 . . 6 103 3 . . 7 103 4 6 9 8 103 5 . . 9",sep="",header=TRUE,na.strings=".",check.names=FALSE)
u3d<- read.table(text="Desired-ID visit AUC Wight ID1 101 3 5 13 1 101 4 10 13 2 101 5 20 13 3 102 3 2 10 4 102 4 4 10 5 102 5 8 10 6 103 3 3 9 7 103 4 6 9 8 103 5 12 9 9",sep="",header=TRUE,check.names=FALSE) u3s1<-unsplit(lapply(split(u3s,u3s$`Current-ID`),function(x) {(x$AUC<-as.integer(x$AUC[!is.na(x$AUC)]/2)* (2^(0:floor(log(4,2))))); x$Wight<- x$Wight[!is.na(x$Wight)];x }),u3s$`Current-ID`) attr(u3s1,"row.names")<- attr(u3d,"row.names") colnames(u3s1)<- colnames(u3d) all.equal(u3s1,u3d) #[1] TRUE A.K. I'm sure I'm missing something really obvious in the "for loop"... Here is simplified data for 3 patients, we need filling in Na's with same WT for each patient, AUC halved for visit 3, doubled for visit 5 for the same patient, based on visit 4 for(i in unique(u3s$ID)){ #fill in same Wt for each patient u3s$WT <- ifelse(is.na(u3s$WT),u3s$WT[u3s$visit == "4"],u3s$WT) for(j in length(u3s$ID1)){ #fill in .5 AUC for visit 3, 2*AUC for visit 5 u3s$AUC24 <- ifelse(is.na(u3s$AUC24),u3s$AUC24[u3s$visit == "4"]*0.5,u3s$AUC24) u3s$AUC24 <- ifelse(!is.na(u3s$AUC24),u3s$AUC24[u3s$visit == "4"]*1.0,u3s$AUC24) u3s$AUC24 <- ifelse(is.na(u3s$AUC24),u3s$AUC24[u3s$visit == "4"]*2.0,u3s$AUC24) } } Current- ID visit AUC Wight ID1 101 3 1 101 4 10 13 2 101 5 3 102 3 4 102 4 4 10 5 102 5 6 103 3 7 103 4 6 9 8 103 5 9 Desired- ID visit AUC Wight ID1 101 3 5 13 1 101 4 10 13 2 101 5 20 13 3 102 3 2 10 4 102 4 4 10 5 102 5 8 10 6 103 3 3 9 7 103 4 6 9 8 103 5 12 9 9 Your help is greatly appreciated... Best Regards ______________________________________________ R-help@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.