-3

I have three variables: Year, Month, and Day. How can I merge them into one variable ("Date") so that the variable is represented as such: yyyy-mm-dd

Thanks in advance and best regards!

  • 3
    Have you tried `paste`? – r2evans Nov 20 '17 at 18:35
  • Please add example data to your question, which contain a minimal reproducible example, e. g. `data <- data.frame(Y = "2010", M = "12", D = "31")` and how exactly your expected result should look alike. It is difficult to find an exact answer otherwise. – R Yoda Nov 20 '17 at 18:49
  • `tidyr::unite(df, "Date", Y:D, sep = "-")` if you don't want to keep the original columns – acylam Nov 20 '17 at 18:52

2 Answers2

0

Below we create year-month-day character strings, yyyy-mm-dd character strings (similar except one digit month and day are zero padded out to 2 digits) and Date class. The last one prints out as yyyy-mm-dd and can be manipulated in ways that character strings can't, for example adding one to a Date class object gives the next day.

First we set up some sample input:

year <- c(2017, 2015, 2014)
month <- c(3, 1, 10)
day <- c(15, 9, 25)

convert to year-month-day character string This is not quite yyyy-mm-dd since 1 digit months and days are not zero padded to 2 digits:

paste(year, month, day, sep = "-")
## [1] "2017-3-15"  "2015-1-9"   "2014-10-25"

convert to Date class It prints on console as yyyy-mm-dd. Two alternatives:

as.Date(paste(year, month, day, sep = "-"))
## [1] "2017-03-15" "2015-01-09" "2014-10-25"

as.Date(ISOdate(year, month, day))
## [1] "2017-03-15" "2015-01-09" "2014-10-25"

convert to character string yyyy-mm-dd In this case 1 digit month and day are zero padded out to 2 characters. Two alternatives:

as.character(as.Date(paste(year, month, day, sep = "-")))
## [1] "2017-03-15" "2015-01-09" "2014-10-25"

sprintf("%d-%02d-%02d", year, month, day)
## [1] "2017-03-15" "2015-01-09" "2014-10-25"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

How do you merge three variables into one variable?

Consider two methods:

  1. Old school

  2. With dplyr, lubridate, and data frames

And consider the data types. You can have:

  1. Number or character
  2. Date or POSIXct final type

Old School Method

The old school method is straightforward. I assume you are using vectors or lists and don't know data frames yet. Let's take your data, force it to a standardized, unambiguous format, and concatenate the data.

> y <- 2012:2015
> y
[1] 2012 2013 2014 2015
> m <- 1:4
> m
[1] 1 2 3 4
> d <- 10:13
> d
[1] 10 11 12 13

Use as.numeric if you want to be safe and convert everything to the same format before concatenation. If you get any NA values you will need to handle them with the is.na function and provide a default value.

Use paste with the sep separator value set to your delimiter, in this case, the hyphen.

> paste(y,m,d, sep = '-')
[1] "2012-1-10" "2013-2-11" "2014-3-12" "2015-4-13"

Dataframe / Dplyr / Lubridate Way

> df <- data.frame(year = y, mon = m, day = d)
> df
  year mon day
1 2012   1  10
2 2013   2  11
3 2014   3  12
4 2015   4  13

Below I do the following:

  1. Take the df object
  2. Create a new variable name Date
  3. Concatenate the numeric variables y, m, and d with a - separator
  4. Convert the string literal into a Date format with ymd()

> df %>% 
  mutate(Date = ymd(
          paste(y,m,d, sep = '-')
                )
        )
  year mon day       Date
1 2012   1  10 2012-01-10
2 2013   2  11 2013-02-11
3 2014   3  12 2014-03-12
4 2015   4  13 2015-04-13
Kamil
  • 412
  • 4
  • 11