Jason Lee
2008-Jun-02 13:45 UTC
[R] Need advise in grab the line number of sorted list in R
Hi, I have a data frame which format is like below:- X Y Z 131 22.2 3.4 4.4 150 20.0 12.2 4.5 etc... And I have sorted the data frame. However, I would like to grab one of these elements in the data frame. Also, i would like to number these rows so that when i grab a particular line let say 150, it is able to return me the position of the row rather than the "150". Let say in this example, when i grab a value of 150, it gives me 2 rather than 150. I tried to add anohter extra column of number which is data$NUMBER row.names(data) on the sorted data but it seems not giving what I wanted which is the "position" of the row 150. Please advise. Thanks. [[alternative HTML version deleted]]
Karl Ove Hufthammer
2008-Jun-02 13:54 UTC
[R] Need advise in grab the line number of sorted list in R
Jason Lee:> I have a data frame which format is like below:- > > X ? ? Y ? ? Z > 131 22.2 3.4 ? 4.4 > 150 ?20.0 12.2 4.5 > > etc... > > And I have sorted the data frame. However, I would like to grab one of > these elements in the data frame. Also, i would like to number these rows > so that when i grab a particular line let say 150, it is able to return me > the position of the row rather than the "150". Let say in this example, > when i grab a value of 150, it gives me 2 rather than 150.I'm not sure I understand what you're trying to do. Could you try to explain using code example of how you 'grab' elements? One solution could perhaps be to simple renumber the data frame. If the data frame is named 'dat', you can do it simply by row.names(dat) = NULL -- Karl Ove Hufthammer
Marc Schwartz
2008-Jun-02 14:02 UTC
[R] Need advise in grab the line number of sorted list in R
on 06/02/2008 08:45 AM Jason Lee wrote:> Hi, > > I have a data frame which format is like below:- > > X Y Z > 131 22.2 3.4 4.4 > 150 20.0 12.2 4.5 > > etc... > > And I have sorted the data frame. However, I would like to grab one of these > elements in the data frame. Also, i would like to number these rows so that > when i grab a particular line let say 150, it is able to return me the > position of the row rather than the "150". Let say in this example, when i > grab a value of 150, it gives me 2 rather than 150. > > > I tried to add anohter extra column of number which is data$NUMBER > row.names(data) on the sorted data but it seems not giving what I wanted > which is the "position" of the row 150. > > Please advise. Thanks.Is this what you want? > DF X Y Z 131 22.2 3.4 4.4 150 20.0 12.2 4.5 > which(row.names(DF) == 150) [1] 2 See ?which HTH, Marc Schwartz