jeudi 2 juin 2016

R reference column while using apply down rows

If I have some data that may be modelled so:

c1 <- c("A","B","C","A")
c2 <- c("a", "a", "b", "c")
c3 <- c(0, 1, 0, 0)
c4 <- c(0, 0, 0, 1)
c5 <- c(1, 0, 0, 0)

my.array <- cbind(c1, c2, c3, c4, c5)

I want to have an array my.array.filled that for columns 3:5 fills from c1 if 0 and c2 if 1 I was thinking of doing this with apply and ifelse but dont know how to refernce the columns with apply(my.array, 2, function(x), ifelse(x == 0, ?, x)) (In this case I would do this twice).

Aucun commentaire:

Enregistrer un commentaire