-1

I have longitudinal patient data in R. I would like to create the new_dummy variable just like I demonstrated in the table below. I would like the dummy_variable value to be 0 if the consultation_date variable is smaller than the registration_date variable and to be 1 if the consultation_date variable is bigger than the registration_date variable.

patid consultation_date registration_date new_dummy
1 07/07/2016 07/07/2018 0
1 07/07/2019 07/07/2018 1
2 14/08/2016 07/09/2016 0
3 07/05/2015 19/02/2016 0
3 02/12/2016 19/02/2016 1
abrar_r
  • 55
  • 5
  • Try `df1$new_dummy <- with(df1, +(as.Date(consultation_date, "%d/%m/%Y") < as.Date(registration_date, "%d/%m/%Y")))` – akrun Jun 28 '22 at 15:34

1 Answers1

0

If your columns are in the right class (Date), you can simply use

df$new_dummy <- as.numeric(df$consultation_date < df$registration_date)

To convert them in the right format, it's:

df$consultation_date <- as.Date(df$consultation_date, "%d/%m/%Y")
df$registration_date <- as.Date(df$registration_date, "%d/%m/%Y")

(thanks @akrun for the date format)

abichat
  • 2,317
  • 2
  • 21
  • 39