rdataframeunique-id

Row bind dataframes and keep unique IDs incrementing


I have 2 datasets that I am about to stack together. Please note that record_ids for the first dataset also start at 1

record_id       any_column
     591             a
     591             b
     591             c
     592             d
     592             e
     592             f
     592             g
     593             h
     593             i
     593             j

2nd dataset

record_id       any_column
     1             a
     1             b
     1             c
     2             d
     2             e
     2             f
     2             g
     3             h
     3             i
     3             j 

when stacking I want record_ids for the 2nd data set to start right where the recod_ids for the 1st dataset ended for instant in this case at 593, I would like to convert ids for the 2nd dataset to something starting at 594 and so forth. How can I best approach this? Thanks

The expectations for the future datasets are as follows:

record_id       any_column        grand_id
     591             a               591
     591             b               591
     591             c               591
     592             d               592
     592             e               592
     592             f               592
     592             g               592
     593             h               593
     593             i               593
     593             j               593
     1             a                 594
     1             b                 594
     1             c                 594
     2             d                 595
     2             e                 595
     2             f                 595
     2             g                 595
     3             h                 596
     3             i                 596
     3             j                 596 

Solution

  • You can get max of record_id from df1 and add to it record_id of df2.

    rbind(transform(df1, grand_id = record_id), 
          transform(df2, grand_id = record_id + max(df1$record_id)))
    

    If you prefer dplyr :

    library(dplyr)
    
    df1 %>%
      mutate(grand_id = record_id) %>%
      bind_rows(df2 %>% mutate(grand_id = record_id + max(df1$record_id)))
    
    #   record_id any_column grand_id
    #1        591          a      591
    #2        591          b      591
    #3        591          c      591
    #4        592          d      592
    #5        592          e      592
    #6        592          f      592
    #7        592          g      592
    #8        593          h      593
    #9        593          i      593
    #10       593          j      593
    #11         1          a      594
    #12         1          b      594
    #13         1          c      594
    #14         2          d      595
    #15         2          e      595
    #16         2          f      595
    #17         2          g      595
    #18         3          h      596
    #19         3          i      596
    #20         3          j      596