mercredi 24 mars 2021

How to create a dummy variable from a date.time column

I am trying to create a new dummy variable that indicates 'before' and 'after' a specific data, there's over a thousand variables so cant do this manually

df:

ID Date_time 1 2010-01-02 00:00:02 2 2010-01-03 00:00:05 3 2010-01-04 00:01:02 .....

I think I need to use the ifelse function to create the new dummy variable to translate as 'if the date is before '2012.03.01' then = 0, if after the date '2012.0301 then =1'.

I have tried this, but doesn't work and just produces NAs

df$dummy < ifelse(df$Date_time < as.Date("2012.03.01", format = "%Y/%M/%D"), 0,1)

Any suggestions please?

The end result should look like

ID Date_time dummy 1 2010-01-02 00:00:02 0 2 2010-01-03 00:00:05 0 3 2010-01-04 00:01:02 0

Aucun commentaire:

Enregistrer un commentaire