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 [[alternative HTML version deleted]] ______________________________________________ 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.