Using row-wise column indices in a vector to extract values from data frame [duplicate]

Just use matrix indexing, like this:

dframe[cbind(seq_along(i), i)]
# [1] "g" "b" "f"

The cbind(seq_along(i), i) part creates a two column matrix of the relevant row and column that you want to extract.

Leave a Comment