I have some date/times in a variable where I would only like to extract the year.
Here is an example:
2/10/21 6:55
I would only like to extract ‘2021’ from this.
By the way, the class for this variable is ‘character’.
> class(df$DTS)
[1] "character"
>Solution :
library(lubridate)
"2/10/21 6:55" |> mdy_hm() |> year()
# [1] 2021