I have a data.frame x in R for which dim(x) = (m,n) and a vector y of numbers of length m and with values between 1 and n. In other words, y has an entry for each row in x and each value is a valid column number in x. I would like to extract a "jagged" column of numbers from x using the column numbers in y. For example, if
y <- c(2,4,1,6,5)
then I would like to get a vector of numbers equal to
c(x[1,2],x[2,4],x[3,1],x[4,6],x[5,5])
What is the most efficient way to do this?
Thanks.