Skip to content
Prev 244208 / 398506 Next

Efficient way to use data frame of indices to initialize matrix

tmpdf <- data.frame( x = c(1,2,3), y=c(2,3,1), a=c(10,20,30) )
mymat <- matrix(0, ncol=3, nrow=3)
mymat[ as.matrix(tmpdf[,c('x','y')]) ] <- tmpdf$a