r user
2006-Mar-14 23:59 UTC
[R] using a value in a column to "lookup" data in a certian column of a dataset?
I have a dataset with 20 columns and ~600,000 rows. Column 1 has a number from 2-19. This number tells me, for each row, which column has the ?applicable? data. (i.e. the data that I wish to use for each individual row) I want to create a vector that contains the data from the value in column 1. e.g. If column 1, row 1, has a value of ?6?, I want to obtain the value in column 6, row1. If column1, row 2, has value of ?2?, I want to obtain the value in column 2, row2. etc I have created a for next loop to do this, but am looking for a more efficient manner. Thanks.
Gabor Grothendieck
2006-Mar-15 00:23 UTC
[R] using a value in a column to "lookup" data in a certian column of a dataset?
>From ?"]" note that:"When indexing arrays by '[' a single argument 'i' can be a matrix with as many columns as there are dimensions of 'x'; the result is then a vector with elements corresponding to the sets of indices in each row of 'i'. " so:> mm <- matrix(1:25,5) > mm[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25> mm[cbind(1:5, mm[,1])][1] 1 7 13 19 25 On 3/14/06, r user <ruser2006 at yahoo.com> wrote:> I have a dataset with 20 columns and ~600,000 rows. > > Column 1 has a number from 2-19. This number tells > me, for each row, which column has the "applicable" > data. (i.e. the data that I wish to use for each > individual row) > > I want to create a vector that contains the data from > the value in column 1. > > e.g. > If column 1, row 1, has a value of "6", I want to > obtain the value in column 6, row1. > > If column1, row 2, has value of "2", I want to obtain > the value in column 2, row2. etc > > > I have created a for next loop to do this, but am > looking for a more efficient manner. > > Thanks. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >