How to convert in both directions between year,month,day and dates in R?

后端 未结 3 1183
悲&欢浪女
悲&欢浪女 2021-01-02 01:34

How to convert between year,month,day and dates in R?

I know one can do this via strings, but I would prefer to avoid converting to strings, partly because maybe the

相关标签:
3条回答
  • 2021-01-02 02:08

    Because there are so many ways in which a date can be passed in from files, databases etc and for the reason you mention of just being written in different orders or with different separators, representing the inputted date as a character string is a convenient and useful solution. R doesn't hold the actual dates as strings and you don't need to process them as strings to work with them.

    Internally R is using the operating system to do these things in a standard way. You don't need to manipulate strings at all - just perhaps convert some things from character to their numerical equivalent. For example, it is quite easy to wrap up both operations (forwards and backwards) in simple functions you can deploy.

    toDate <- function(year, month, day) {
        ISOdate(year, month, day)
    }
    
    toNumerics <- function(Date) {
        stopifnot(inherits(Date, c("Date", "POSIXt")))
        day <- as.numeric(strftime(Date, format = "%d"))
        month <- as.numeric(strftime(Date, format = "%m"))
        year <- as.numeric(strftime(Date, format = "%Y"))
        list(year = year, month = month, day = day)
    }
    

    I forego the a single call to strptime() and subsequent splitting on a separation character because you don't like that kind of manipulation.

    > toDate(2004, 12, 21)
    [1] "2004-12-21 12:00:00 GMT"
    > toNumerics(toDate(2004, 12, 21))
    $year
    [1] 2004
    
    $month
    [1] 12
    
    $day
    [1] 21
    

    Internally R's datetime code works well and is well tested and robust if a bit complex in places because of timezone issues etc. I find the idiom used in toNumerics() more intuitive than having a date time as a list and remembering which elements are 0-based. Building on the functionality provided would seem easier than trying to avoid string conversions etc.

    0 讨论(0)
  • 2021-01-02 02:11

    I'm a bit late to the party, but one other way to convert from integers to date is the lubridate::make_date function. See the example below from R for Data Science:

    library(lubridate)
    library(nycflights13)
    library(tidyverse)
    
    a <- flights %>%
      mutate(date = make_date(year, month, day))
    
    0 讨论(0)
  • 2021-01-02 02:27

    Found one solution for going from date to year,month,day.

    Let's say we have a date object, that we'll create here using ISOdate:

    somedate <- ISOdate(2004,12,21)
    

    Then, we can get the numerical components of this as follows:

    unclass(as.POSIXlt(somedate))
    

    Gives:

    $sec
    [1] 0
    
    $min
    [1] 0
    
    $hour
    [1] 12
    
    $mday
    [1] 21
    
    $mon
    [1] 11
    
    $year
    [1] 104
    

    Then one can get what one wants for example:

    unclass(as.POSIXlt(somedate))$mon
    

    Note that $year is [actual year] - 1900, month is 0-based, mday is 1-based (as per the POSIX standard)

    0 讨论(0)
提交回复
热议问题