r extract year from string code example
Example: extract year from date in r
#create data frame
df <- data.frame(date=c("02/10/2021", "03/07/2020" , "14/03/2021"),
sales=c(21, 36, 38))
#view data frame
df
date sales
1 02/10/2021 21
2 03/07/2020 36
3 14/03/2021 38
#create new variable that contains year
df$year <- format(as.Date(df$date, format="%d/%m/%Y"),"%Y")