Search code examples
rrow

select row based on value of another row in R


EDIT: to make myself clear, I know how to select individual rows and I know there are many different ways of doing it. I want to write a code that will work no matter what the actual value of the rows is, so it works over a larger dataframe, that is, I don't have to change the code based on the content. So instead of saying, select row 1, then 3, it'll say, select row one, then row [value in row 1 column Z] then row [value in column Z from the row just selected] and so on - so my question is, how to tell R to read that value as row number

I'm trying to figure out how to select and save a row based on a value in another row, so that I get get a new df with row 1(aA), then go to row 3 and save it (cC), then go to row 2 etc.

X  Y  Z 

a  A  3
b  B  5
c  C  2
d  D  1
e  E  NA

Knowing the row number, I can use rbind which will give me the following

rbind(df[1, ], df[3, ]

a A 3
c C 2

But I want R to extract the number 3 from the column not to explicitly tell it which row to pick - how do I do that? Thanks


Solution

  • You can use a while loop to keep on selecting rows until NA occurs or all the rows are selected in the dataframe.

    all_rows <- 1
    next_row <- df$Z[all_rows]
    
    while(!is.na(next_row) || length(all_rows) >= nrow(df)) {
      all_rows <- c(all_rows, next_row)
      next_row <- df$Z[all_rows[length(all_rows)]]
    }
    
    result <- df[all_rows, ]
    
    #  X Y  Z
    #1 a A  3
    #3 c C  2
    #2 b B  5
    #5 e E NA