Order a data frame based on a column that points to the next record

I have a data frame with a column that points to the next record, sample dataframe below.

OG_Data <- data.frame(
  Record = c("aaaa", "NNNN", "rrrr", "tttt", "pppp", "ssss", "bbbb"),
  NextRecord = c("pppp", "tttt", "bbbb", "N/A" , "NNNN", "rrrr", "N/A") 
)
# Record NextRecord 
#   aaaa       pppp
#   NNNN       tttt       
#   rrrr       bbbb       
#   tttt        N/A        
#   pppp       NNNN       
#   ssss       rrrr       
#   bbbb        N/A        

I want to order this data frame based on a predefined sequence determined by column B (NextRecord) that points to the next record's column A (Record) to get the sequence order and line group.

Desired Output:

# Record  NextRecord  Sequence  Line 
#   aaaa        pppp         1     1    
#   pppp        NNNN         2     1    
#   NNNN        tttt         3     1    
#   tttt         N/A         4     1    
#   ssss        rrrr         1     2    
#   rrrr        bbbb         2     2    
#   bbbb         N/A         3     2    

I was thinking of something like this:

OG_Data[1,] %>%
  add_row(OG_Data, filter(OG_Data, OG_Data$Record == NextRecord))

But that doesn't work and is not scalable. Also, I am not sure where to start to find the beginning of the line groups.



Comments

Popular posts from this blog

I get wrong characters when retreiving the message body of an email using TIdIMAP4.UIDRetrieveTextPeek2()

How to drop the all the 1's in a correlation matrix

Today Walkin 14th-Sept