Search code examples
rdateposixct

Splitting created sequence of dates into separate columns


I created a dataframe (Dates) of dates/times every 3 hours from 1981-2010 as follows:

# Create dates and times
start <- as.POSIXct("1981-01-01")
interval <- 60
end <- start + as.difftime(10957, units="days")
Dates = data.frame(seq(from=start, by=interval*180, to=end))
colnames(Dates) = "Date"

I now want to split the data into four separate columns with year, month, day and hour. I tried so split the dates using the following code:

Date.split = strsplit(Dates, "-| ")

But I get the following error:

Error in strsplit(Dates, "-| ") : non-character argument

If I try to convert the Dates data to characters then it completely changes the dates, e.g.

Dates.char = as.character(Dates)

gives the following output:

Dates.char Large Character (993.5 kB)
   chr "c(347155200, 347166000 ...

I'm getting lost with the conversion between character and numeric and don't know where to go from here. Any insights much appreciated.


Solution

  • One way is to use format.

    head(
      setNames(
        cbind(Dates, 
          format(Dates, "%Y"), format(Dates, "%m"), format(Dates, "%d"),
          format(Dates, "%H")), 
        c("dates", "year", "month", "day", "hour"))
    )
                    dates year month day hour
    1 1981-01-01 00:00:00 1981    01  01   00
    2 1981-01-01 03:00:00 1981    01  01   03
    3 1981-01-01 06:00:00 1981    01  01   06
    4 1981-01-01 09:00:00 1981    01  01   09
    5 1981-01-01 12:00:00 1981    01  01   12
    6 1981-01-01 15:00:00 1981    01  01   15