Home > Back-end >  Extract year YYYY from date DD.MM.YYYY as new variable in R
Extract year YYYY from date DD.MM.YYYY as new variable in R

Time:01-25

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")

CodePudding user response:

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

CodePudding user response:

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
  • Related