Consider the data.frame:

df <- data.frame(A = c(1,4,2,6,7,3,6), B= c(3,7,2,7,3,5,4), C =
c(2,7,5,2,7,4,5), index = c("A","B","A","C","B","B","C"))

I want to select the column specified in 'index' for every row of 'df', to
get

goal <- c(1, 7, 2, 2, 3, 5, 5)

This sounds a lot like the indexing-by-a-matrix you can do with arrays;

df[cbind(1:nrow(df), df$index)]

but this returns me values that are all characters where I want numbers.
(it seems that indexing by an array isn't well supported for data.frames.)

What is a better way to perform this selection operation?

Peter

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to