mercredi 1 août 2018

Get the next row after a specific one in R?

Rank   Values
1      e
2      er
3      gr
4      ie
5      wv
1      iop
1      ni
2      ot
3      kr
4      lw

I have a column "rank" in a data frame and I want add get the rows that are 1 and check to see if they have a 2 after. How can I do this within a for loop? I know this can be done with an if statement I am just not sure how to grab the correct rows? I need to basically delete the rows that are ranked 1 and do not have a following rank 2.

Example Output:

Rank   NewCol
1      e       
2      er
3      gr
4      ie
5      wv
1      ni
2      ot
3      kr
4      lw

(Got rid of iop since its rank is 1 with no following 2)

Aucun commentaire:

Enregistrer un commentaire