mercredi 31 juillet 2019

how to extract and change the date in date field if a specific condition is met

I've a dataframe as under

season  date
9090-2019fall   1/1/00
9879-2018fall   2/2/01
9801-2018spring 3/3/03
7868-2017fall   4/4/04
5765-2020fall   5/5/05

I'm tying to extract the year from the season column and then change the date in the date column where year < todays year The new date should be the first day in the month of may for the extracted year

I came across a solution to do it manually for each year

This is where I've gotten so far

df$date<-ifelse(grepl('2019fall', df$season, paste0('2019-05-01'),df$date)

Is there a way to have it run it in a loop instead of doing it manually ?

My expected result is as under

season  date
9090-2019fall   5/1/19
9879-2018fall   5/1/18
9801-2018spring 3/3/03
7868-2017fall   5/1/17
5765-2020fall   5/5/05

Aucun commentaire:

Enregistrer un commentaire