0

I have a df with three columns (among others) with "date-data": day, month and year. I am wondering if there is any clever way to join them to one column with "DD-MM_YYYY" instead? Perhaps with lubridate?

`day<-c(2, 4, 22, 7, 17, 12)
month<-c(2, 1, 6, 12, 10, 5)
year<-c(1999, 2000, 2001, 1977, 1982, 1940)
df<-data.frame(day, month, year)`

I have only joined string columns before or created column where I have done computations. This is new to me.

Mactilda
  • 393
  • 6
  • 18

2 Answers2

1

Since you mentioned lubridate, how about this?

library(tidyverse)
library(lubridate)
df %>%
    unite(dmy, day, month, year) %>%
    transmute(dmy = dmy(dmy))
#         dmy
#1 1999-02-02
#2 2000-01-04
#3 2001-06-22
#4 1977-12-07
#5 1982-10-17
#6 1940-05-12
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0
df$date <- as.Date(with(df, paste(day, month, year,sep="-")), "%d-%m-%y")     
Mateusz1981
  • 1,817
  • 17
  • 33
Aditya
  • 38
  • 8