Search code examples
runiqueidentifier

Assigning Unique Values


I have a data frame (02outcomes$transfusion_date) in which I have a series of dates that look like this:

2020-03-28 18:01:00
2020-03-28 22:16:00
2020-04-01 19:30:00
2020-04-01 23:56:00

I would like to assign unique IDs to each like this:

1   2020-03-28 18:01:00
2   2020-03-28 22:16:00
3   2020-04-01 19:30:00
4   2020-04-01 23:56:00

The values need to be in chronological order, ascending.

On a side note, when I view my data using the dput function it looks like this, is that problematic?:

1588635000, 1588706160, 1588714320, 1588721640, 1588881900 

I found a thread that seems to be similar but cannot seem to make these solutions work: Create an ID (row number) column

Thanks in advance


Solution

  • arrange the data by transfusion_date and then you can create a unique index for each row.

    In base R, you can do :

    outcomes <- outcomes[order(outcomes$transfusion_date), ]
    outcomes$ID <- seq_len(nrow(outcomes))
    

    Or using dplyr :

    library(dplyr)
    outcomes %>% arrange(transfusion_date) %>% mutate(Id = row_number())