[R] Help with code

Rui Barradas ruipbarradas at sapo.pt
Fri Dec 23 23:20:15 CET 2011


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.



More information about the R-help mailing list