mercredi 25 août 2021

Why do I get error when trying to create variable with ifelse statement based on date?

I'm pretty new to R coding so please excuse me if this is a dumb question. I'm trying to create a new column in my data frame "CPINSA" named "weight_year". Using an ifelse statement, I'm trying to say "if the date in "REF_DATE" is later than June 1st 2021, then the value should be 2020, if not it should be blank". Here's the code I'm using:

CPINSA$weight_year <- ifelse((CPINSA$REF_DATE >= as.Date('2021-06-01')),2020,"")

I keep getting this error:

Error in `$<-.data.frame`(`*tmp*`, weight_year, value = logical(0)) : 
  replacement has 0 rows, data has 1031379

Can anyone tell me why and what to do?

Thanks!

Aucun commentaire:

Enregistrer un commentaire