*Hi R Users, * *I was trying to write a simple function for data imputation. I need to index the results from each step and combine it with some data of the original dataset for the next step, until the end. I attached a sample data and the work-well R code function I wrote, but the function is rather applied easily for all kind of data. Can somebody generalize my 'pace.imp' function? * * run the attached sample data using the following function: pred<-matrix(c(rep(0, 11), rep(1,7), rep(0, 72)), 9, 9, byrow=T) rownames(pred)<-colnames(pred)<-paste("PR", 1:9) impace<-function(data, time, imp, i, pred, m){ library(mice) if (sum(is.na(data[data[, time]<=i, imp]))>=1) {compl<-complete(mice(data[data[, time]<=i, ], pred=pred, m=m))} else {compl=data[data[, time]<=i, ]} return(compl) } pace.imp<-function(data, time, imp, pred, m){ C1<-impace(data, time, imp, 1, pred, m) R1<-rbind(C1, data[data[, time]==(1+1),]) C2<-impace(R1, time, imp, 2, pred, m) R2<-rbind(C2, data[data[, time]==(1+2),]) C3<-impace(R2, time, imp, 3, pred, m) R3<-rbind(C3, data[data[, time]==(1+3),]) C4<-impace(R3, time, imp, 4, pred, m) R4<-rbind(C4, data[data[, time]==(1+4),]) C5<-impace(R4, time, imp, 5, pred, m) R5<-rbind(C5, data[data[, time]==(1+5),]) C6<-impace(R5, time, imp, 6, pred, m) R6<-rbind(C6, data[data[, time]==(1+6),]) C7<-impace(R6, time, imp, 7, pred, m) R7<-rbind(C7, data[data[, time]==(1+7),]) C8<-impace(R7, time, imp, 8, pred, m) R8<-rbind(C8, data[data[, time]==(1+8),]) C9<-impace(R8, time, imp, 9, pred, m) R9<-rbind(C9, data[data[, time]==(1+9),]) C10<-impace(R9, time, imp, 10, pred, m) R10<-rbind(C10, data[data[, time]==(1+10),]) C11<-impace(R10, time, imp, 11, pred, m) return(C11) } Pace<-pace.imp(test, time=4, imp=2, pred, 10) * *Thanks, * *David * -------------- next part -------------- "PR 1" "PR 2" "PR 3" "PR 4" "PR 5" "PR 6" "PR 7" "PR 8" "PR 9" "694" 1196 1 2 1 237 3 4 3 4 "731" 1225 NA 1 1 281 1 2 2 3 "739" 1228 0 2 1 261 2 2 1 1 "758" 1024 NA 1 1 462 1 2 1 1 "766" 1083 0 1 1 307 2 3 2 2 "88" 1067 NA 1 2 412 2 2 1 1 "119" 1090 NA 1 2 347 1 2 1 1 "196" 1132 0 2 2 319 3 2 1 1 "222" 1139 0 1 2 318 3 3 2 2 "251" 1153 0 2 2 329 2 2 2 2 "358" 1199 NA 2 2 201 1 2 1 1 "397" 1214 NA 1 2 290 1 2 1 1 "458" 1007 NA 2 2 317 1 2 2 3 "479" 1018 1 1 2 259 2 3 3 4 "496" 1022 0 1 2 410 1 1 1 1 "528" 1045 NA 1 2 304 1 2 1 1 "571" 1085 NA 1 2 308 1 2 2 3 "593" 1117 0 1 2 444 2 2 1 1 "601" 1119 0 2 2 344 3 2 1 1 "644" 1147 0 1 2 253 4 4 1 1 "659" 1167 0 1 2 224 2 2 1 1 "680" 1181 0 1 2 303 3 2 3 2 "695" 1196 NA 2 2 252 1 3 1 1 "732" 1225 NA 1 2 321 1 2 1 1 "740" 1228 0 2 2 348 2 2 1 1 "759" 1024 0 1 2 482 2 2 3 2 "767" 1083 NA 1 2 356 1 2 1 1 "120" 1090 0 1 3 446 4 2 2 2 "197" 1132 0 2 3 330 3 2 1 1 "223" 1139 0 1 3 332 2 4 3 3 "252" 1153 0 2 3 348 1 2 1 1 "359" 1199 NA 2 3 235 1 2 3 2 "398" 1214 0 1 3 332 1 2 1 1 "459" 1007 0 2 3 326 2 3 1 1 "480" 1018 0 1 3 272 2 2 3 3 "497" 1022 NA 1 3 431 1 1 1 1 "529" 1045 0 1 3 377 1 2 1 1 "572" 1085 0 1 3 326 2 2 1 1 "602" 1119 0 2 3 375 2 2 1 1 "645" 1147 0 1 3 303 1 2 1 1 "660" 1167 1 1 3 246 2 3 1 1 "681" 1181 NA 1 3 366 2 2 1 1 "696" 1196 0 2 3 274 3 2 1 1 "741" 1228 0 2 3 368 1 2 1 1 "768" 1083 0 1 3 375 1 2 2 2 "198" 1132 NA 2 4 422 1 2 1 1 "224" 1139 NA 1 4 345 1 2 1 1 "253" 1153 0 2 4 386 1 2 2 2 "360" 1199 NA 2 4 258 1 2 1 1 "399" 1214 0 1 4 348 1 2 1 1 "460" 1007 NA 2 4 335 1 3 2 4 "481" 1018 0 1 4 319 2 2 2 2 "498" 1022 0 1 4 467 1 2 1 1 "530" 1045 NA 1 4 500 2 2 2 2 "573" 1085 0 1 4 383 1 2 1 1 "603" 1119 0 2 4 457 2 2 1 1 "646" 1147 0 1 4 326 2 3 1 1 "661" 1167 0 1 4 255 1 3 1 1 "697" 1196 NA 2 4 288 1 2 1 1 "769" 1083 1 1 4 479 1 2 1 1 "225" 1139 NA 1 5 442 1 2 1 1 "254" 1153 0 2 5 406 2 2 1 1 "361" 1199 NA 2 5 280 1 2 1 1 "400" 1214 NA 1 5 357 1 2 1 1 "461" 1007 0 2 5 340 2 3 2 2 "482" 1018 0 1 5 326 1 2 1 1 "574" 1085 0 1 5 472 4 2 1 1 "647" 1147 0 1 5 387 2 3 1 1 "662" 1167 0 1 5 284 2 3 1 1 "698" 1196 0 2 5 296 2 2 1 1 "362" 1199 0 2 6 304 3 2 2 2 "462" 1007 1 2 6 396 2 3 3 2 "483" 1018 0 1 6 344 4 3 4 2 "663" 1167 0 1 6 296 2 2 1 1 "699" 1196 NA 2 6 311 1 2 1 1 "363" 1199 NA 2 7 334 1 2 1 1 "484" 1018 0 1 7 367 1 4 3 2 "664" 1167 0 1 7 314 2 2 1 1 "700" 1196 0 2 7 373 2 2 1 1 "485" 1018 0 1 8 386 2 2 1 1 "665" 1167 0 1 8 359 2 2 1 1 "701" 1196 NA 2 8 386 1 2 1 1 "486" 1018 0 1 9 411 2 2 1 1 "666" 1167 NA 1 9 401 1 2 1 1 "487" 1018 0 1 10 432 2 2 3 3 "488" 1018 0 1 11 500 2 2 1 1