r - Get specific column value for each row -


i want "m" length vector that, considering m x n matrix, each row, gives value on column identified column (say column "z"). made using loop:

for (i in 1:dim(data.frame)[1]){vector[i] <- data.frame[i,data.frame$z[i]]} 

do see simpler way code avoiding loop?

"apply" possibility:

> m <- cbind( matrix(1:15,3,5), "z"=c(3,1,2) )  > m                  z [1,] 1 4 7 10 13 3 [2,] 2 5 8 11 14 1 [3,] 3 6 9 12 15 2  > v <- apply(m,1,function(x){x[x["z"]]})  > v [1] 7 2 6 >  

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -