Search code examples
rdatemergecol

merge colums with numbers and strings in R


I have 3 different datasets, in two of them, there are columns "day", "month" and "year" like 1,January and 2016 and in the third one there is the column "date" like 1-1-2016. I want to merge the 3 columns in the two datasets into a new one date column like the third dataset so as I can merge the 3 datasets into one. Any ideas?


Solution

  • Paste the the columns and convert to a date:

    df$date <- as.Date( paste( df$day, df$month, df$year , sep = "-" ) , format = "%d-%m-%Y" )
    

    From the tidyr package:

    library(tidyr)
    df <- df %>% unite( col = date, day, month, year, sep = "-")