[R] indexing data.frame columns

ilai keren at math.montana.edu
Thu Apr 5 22:41:31 CEST 2012


On Thu, Apr 5, 2012 at 1:40 PM, Peter Meilstrup
<peter.meilstrup at gmail.com> wrote:
> 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.

str(df[,-4][cbind(1:nrow(df),df$index)])
 num [1:7] 1 7 2 2 3 5 5

> (it seems that indexing by an array isn't well supported for data.frames.)

No, it's just that the index column in df is a factor so as.matrix(df)
return a matrix of characters

>
> What is a better way to perform this selection operation?
>

Not that I know of

Cheers


> Peter
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list