Home > Back-end >  Create column based on other Date column
Create column based on other Date column

Time:11-17

Hi I have this column in my data

df <- structure(list(validation_date = structure(c(18817, 18818, 18820, 
18820, 18821, 18842, 18843, 18843, 18843, 18843, 18843, 18890, 
18890, 18890, 18893, 18893, 18893, 18858, 18858, 18858, 18858, 
18858, 18859, 18831, 18831, 18831, 18831, 18831, 18834, 18830, 
18830, 18830, 18830, 18830, 18831, 18887, 18890, 18890, 18890, 
18890, 18890), class = "Date")), row.names = c(NA, -41L), class = c("tbl_df", 
"tbl", "data.frame"))

I would to create a new column called classifiers with 3 values, conditioned by column validation_date in which dates before 2021-07-26 has the name classifier_1, dates after the 2021-07-26 and before 2021-08-19 has the name classifier_2 and the dates after 19-08-2021 has the name classifier_3.

Thank's

CodePudding user response:

Please find below a solution using data.table

Reprex

  • Code
library(data.table)
library(lubridate)


setDT(df)[, classifiers := fcase(validation_date < ymd("2021-07-26"), "classifier_1",
                                 validation_date >= ("2021-07-26") & validation_date < ymd("2021-08-19"), "classifier_2",
                                 validation_date >= ymd("2021-08-19"), "classifier_3")][]
  • Output
#>     validation_date  classifiers
#>  1:      2021-07-09 classifier_1
#>  2:      2021-07-10 classifier_1
#>  3:      2021-07-12 classifier_1
#>  4:      2021-07-12 classifier_1
#>  5:      2021-07-13 classifier_1
#>  6:      2021-08-03 classifier_2
#>  7:      2021-08-04 classifier_2
#>  8:      2021-08-04 classifier_2
#>  9:      2021-08-04 classifier_2
#> 10:      2021-08-04 classifier_2
#> 11:      2021-08-04 classifier_2
#> 12:      2021-09-20 classifier_3
#> 13:      2021-09-20 classifier_3
#> 14:      2021-09-20 classifier_3
#> 15:      2021-09-23 classifier_3
#> 16:      2021-09-23 classifier_3
#> 17:      2021-09-23 classifier_3
#> 18:      2021-08-19 classifier_3
#> 19:      2021-08-19 classifier_3
#> 20:      2021-08-19 classifier_3
#> 21:      2021-08-19 classifier_3
#> 22:      2021-08-19 classifier_3
#> 23:      2021-08-20 classifier_3
#> 24:      2021-07-23 classifier_1
#> 25:      2021-07-23 classifier_1
#> 26:      2021-07-23 classifier_1
#> 27:      2021-07-23 classifier_1
#> 28:      2021-07-23 classifier_1
#> 29:      2021-07-26 classifier_2
#> 30:      2021-07-22 classifier_1
#> 31:      2021-07-22 classifier_1
#> 32:      2021-07-22 classifier_1
#> 33:      2021-07-22 classifier_1
#> 34:      2021-07-22 classifier_1
#> 35:      2021-07-23 classifier_1
#> 36:      2021-09-17 classifier_3
#> 37:      2021-09-20 classifier_3
#> 38:      2021-09-20 classifier_3
#> 39:      2021-09-20 classifier_3
#> 40:      2021-09-20 classifier_3
#> 41:      2021-09-20 classifier_3
#>     validation_date  classifiers

Created on 2021-11-16 by the reprex package (v2.0.1)

  • Related