Skip to content
Prev 316725 / 398506 Next

vectorisation

Hello,

Try the following.

set.seed(4315)
ml <- data.frame(matrix(sample(1:50,80, replace=TRUE),20,4))
mm <- apply(ml, 2, cumsum)
s2 <- starts <- data.frame(matrix(0,600,4))

for (i in 1:4){
	starts[,i][mm[,i]] <- 1
}

s2[] <- lapply(seq_len(ncol(mm)), function(i) {s2[,i][mm[,i]] <- 1; s2[,i]})

identical(s2, starts) # TRUE


Note that lapply is a loop in disguise.

Hope this helps,

Rui Barradas

Em 02-02-2013 16:38, Brett Robinson escreveu: