Hello, There are so many people posting answers that I'm curious and decided to try one.
I don't know if this is it but it doesn't give an error and it reformats your data according to the rules in your original code. # nr <- dim(c1)[1] nc <- dim(c1)[2] c2 <- NULL c2_row <- rep("", nc-1) for(i in 1:nr){ ptype <- as.character(c1$t1d_ptype[i]) stype <- substr(ptype,1,4) num_comp <- sum(c1[i,] == "Y") for(j in 1:(nc-1)){ c2_row[j] <- "" if(num_comp > 0){ c1_tmp <- as.integer(c1[i ,j]) if(ptype == "T1D"){ if(c1_tmp == 1) c2_row[j] <- "T1D_oc" if(c1_tmp == 2) c2_row[j] <- "T1D_w" } if(stype == "Ctrl"){ if(c1_tmp == 1) c2_row[j] <- "Ctrl_oc" if(c1_tmp == 2) c2_row[j] <- "Ctrl_w" } } else{ if(ptype == "T1D") c2_row[j] <- "T1D_noc" if(stype == "Ctrl") c2_row[j] <- "Ctrl_noc" } } c2 <- rbind(c2, c(c2_row, ptype)) } c2 <- data.frame(c2) colnames(c2) <- colnames(c1) c2 I bet there's a way to work on entire objects. This is C-like. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4229987.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.