0

I have a dataframes that is a bootstrapped sample. This means some IDs have been repeated. Each dataframe contains 3 columns: ID, YEAR, Short_Rating.

My subsequent code requires the IDs to be unique. I need to rename the IDs and make them unique. For example I will have the following dataframe:

 ID              YEAR       Short_Rating
4205              1996          1
4205              1997          2
4205              1998          5        
4205              1999          2
2356              1996          3
2356              1997          4
2356              1998          1        
2356              1999          2
4205              1996          1
4205              1997          2
4205              1998          5        
4205              1999          2

I want it to rename the IDs in the ID column to:

   ID              YEAR       Short_Rating
    1              1996          1
    1              1997          2
    1              1998          5        
    1              1999          2
    2              1996          3
    2              1997          4
    2              1998          1        
    2              1999          2
    3              1996          1
    3              1997          2
    3              1998          5        
    3              1999          2

Each ID will always consist of the same number of years.

gm007
  • 547
  • 4
  • 11
  • `df$ID <- match(df$ID, unique(df$ID))` – Ronak Shah Jul 07 '20 at 03:49
  • 1
    This does not match the desired result. The second block of 4205 should be 3. – dcarlson Jul 07 '20 at 03:52
  • Are the IDs always in blocks of 4? If so, something like n <- nrow(df)/4; df$ID <- rep(1:n, each=4). – dcarlson Jul 07 '20 at 03:57
  • They will be 155 but I used a shorter example above. Your suggestion repeated the wrong sequence. I used this and it worked. n <- nrow(df)/4; df$ID <- rep(1:4, each=n). Thank you – gm007 Jul 07 '20 at 04:07

0 Answers0