1

I have the following example dataset and I would like to transform each date group into an integer with the earliest date starting at 1 and successively growing for each new date group. Any help would be greatly appreacited.

dateA = c("5/8/2020","5/8/2020", "5/8/2020", "5/9/2020","5/9/2020", "5/9/2020", "5/9/2020", "5/10/2020","5/10/2020", "5/10/2020", "5/11/2020")
varA = c(2,3,4,9,10,11,12,20,21,22,55)
data = data.frame(dateA,varA)
Oringal Index
dateA     varA
5/8/2020  2
5/8/2020  3
5/8/2020  4
5/9/2020  9
5/9/2020  10
5/9/2020  11
5/9/2020  12
5/10/2020 20
5/10/2020 21
5/10/2020 22
5/11/2020 55

Wanted Result
dateA     varA
5/8/2020  1
5/8/2020  1
5/8/2020  1
5/9/2020  2
5/9/2020  2
5/9/2020  2
5/9/2020  2
5/10/2020 3
5/10/2020 3
5/10/2020 3
5/11/2020 4

Current Attempt

library(dplyr)
data = data %>%
  mutate(varA = as.integer(factor(dateA)))

      dateA varA
1   5/8/2020    3
2   5/8/2020    3
3   5/8/2020    3
4   5/9/2020    4
5   5/9/2020    4
6   5/9/2020    4
7   5/9/2020    4
8  5/10/2020    1
9  5/10/2020    1
10 5/10/2020    1
11 5/11/2020    2

MrClean
  • 1,300
  • 2
  • 12
  • 17

1 Answers1

2

An option is match

library(dplyr)
 data %>% mutate(varA = match(dateA, unique(dateA)))

Or with factor with levels specified

data %>%
    mutate(varA = as.integer(factor(dateA, levels = unique(dateA)))
akrun
  • 874,273
  • 37
  • 540
  • 662