[R] indexing data.frame columns

Milan Bouchet-Valat nalimilan at club.fr
Thu Apr 5 22:42:44 CEST 2012


Le jeudi 05 avril 2012 à 12:40 -0700, Peter Meilstrup a écrit :
> 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?
I think the problem is that the data frame is converted to a matrix
under the hood, so numeric values are converted to characters (since the
reverse is not possible). You can either do:
as.numeric(df[cbind(1:nrow(df), df$index)])
[1] 1 7 2 2 3 5 5

Or avoid the conversion by excluding the character column beforehand:
df[-ncol(df)][cbind(1:nrow(df), df$index)]
[1] 1 7 2 2 3 5 5


Regards



More information about the R-help mailing list