1

I have a field (variable) data2$DatumZalozenia with dates as follows 06.12.2013. Format of the values in the variable is DD.MM.YYYY. The variable is type chr. I need to extract the year out of the date and create new variable data2$RokZalozenia in a format YYYY. I need to do that in R. Can somebody please help me on this?

data2$RokZalozenia <- as.Date((data2$DatumZalozenia), format = "%d.%m,%Y")
jpsmith
  • 11,023
  • 5
  • 15
  • 36

2 Answers2

2

Using R base sub

> df$newDate <-sub(".*(\\d{4})$", "\\1", df$date1)
> df
       date1 newDate
1 06.12.2013    2013
2 06.12.2014    2014
3 06.12.2015    2015

data from @jpsmith

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • 1
    I would favor this solution, especially since OP has the dates as character and only needs the year. It makes no sense to first convert it to a date first and then extract the year from that. – Merijn van Tilborg Jan 24 '23 at 16:08
  • 1
    @MerijnvanTilborg I agree - this is much more elegant and pragmatic than my solution, though the tools used in the date-conversion could work for other/future readers who have different data formats, so will leave mine. – jpsmith Jan 24 '23 at 16:11
1

You can use the lubridate package functions year and mdy:

x <- "06.12.2013"
lubridate::year(lubridate::mdy(x))
# [1] 2013

Or in base R:

format(as.Date(x, format = "%d.%m.%Y"), "%Y")
# [1] "2013"

For the application in a data frame to create a new variable:

df <- data.frame(date1 = c("06.12.2013", "06.12.2014", "06.12.2015"))
# Lubridate approach
df$newDateLubridate <- lubridate::year(lubridate::mdy(df$date1))
# Base R approach
df$newDateBaseR <- format(as.Date(df$date1, format = "%d.%m.%Y"), "%Y")

#        date1 newDateLubridate newDateBaseR
# 1 06.12.2013             2013         2013
# 2 06.12.2014             2014         2014
# 3 06.12.2015             2015         2015
jpsmith
  • 11,023
  • 5
  • 15
  • 36