Split column of format YYYY-MM-DD HH:MM:SS to two (date object) columns YYYY-MM-DD and HH:MM:SS in R

StackOverflow https://stackoverflow.com/questions/16488693

  •  21-04-2022
  •  | 
  •  

Frage

I need to segregate time and date from a data.frame that has time column in the form 2012-09-27 07:05:59. I then have to use date and time columns to extract data of a particular date/time. How do I go about doing this? May be I want to do the reverse of this one.

I tried using strptime function and lubridate package but could not get it to work.

data1 <- structure(list(event.date = structure(list(sec = c(59, 29, 59, 
0, 29, 59, 29, 29, 59, 59), min = c(5L, 7L, 15L, 17L, 17L, 19L, 
21L, 22L, 22L, 23L), hour = c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L), mday = c(27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 
27L), mon = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), year = c(112L, 
112L, 112L, 112L, 112L, 112L, 112L, 112L, 112L, 112L), wday = c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), yday = c(270L, 270L, 270L, 
270L, 270L, 270L, 270L, 270L, 270L, 270L), isdst = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("sec", "min", "hour", 
"mday", "mon", "year", "wday", "yday", "isdst"), class = c("POSIXlt", 
"POSIXt"), tzone = c("CST", "GMT", "   ")), VE = c(45L, 55L, 
45L, 50L, 55L, 35L, 30L, 45L, 55L, 30L)), .Names = c("event.date", 
"VE"), row.names = c(NA, 10L), class = "data.frame")

The following gives me NAs:

as.Date(as.character(data1$event.date),"%HH:%MM:%SS")

I am also not able to use lubridate successfully...

ymd_hms(as.character(data1$event.date))

I am confused about which date format/package I should use.

War es hilfreich?

Lösung

Here you go:

R> data1$pt <- as.POSIXct(data1[,1])              # parse time from ISO string
R> data1$date <- as.Date(data1[,"pt"])            # transform to Date
R> data1$time <- format(data1[,"pt"], "%H:%M:%S") # transform to time string
R> data1
            event.date VE                  pt       date     time
1  2012-09-27 07:05:59 45 2012-09-27 07:05:59 2012-09-27 07:05:59
2  2012-09-27 07:07:29 55 2012-09-27 07:07:29 2012-09-27 07:07:29
3  2012-09-27 07:15:59 45 2012-09-27 07:15:59 2012-09-27 07:15:59
4  2012-09-27 07:17:00 50 2012-09-27 07:17:00 2012-09-27 07:17:00
5  2012-09-27 07:17:29 55 2012-09-27 07:17:29 2012-09-27 07:17:29
6  2012-09-27 07:19:59 35 2012-09-27 07:19:59 2012-09-27 07:19:59
7  2012-09-27 07:21:29 30 2012-09-27 07:21:29 2012-09-27 07:21:29
8  2012-09-27 07:22:29 45 2012-09-27 07:22:29 2012-09-27 07:22:29
9  2012-09-27 07:22:59 55 2012-09-27 07:22:59 2012-09-27 07:22:59
10 2012-09-27 07:23:59 30 2012-09-27 07:23:59 2012-09-27 07:23:59
R> 

And here are your column types, showing that I changed POSIXlt to POSIXct with the first command:

R> sapply(data1, class)
$event.date
[1] "POSIXlt" "POSIXt" 

$VE
[1] "integer"

$pt
[1] "POSIXct" "POSIXt" 

$date
[1] "Date"

$time
[1] "character"

R> 

Andere Tipps

My understanding from the comments is that the data you get actually looks like data2. Using that original data we can get data3 which has a "POSIXct" class datetime column, a "Date" class date column and a "character" class time column:

data2 <- transform(data1, 
    event.date = factor(format(event.date, "%m/%d/%Y %H:%M:%S")))

data3 <- transform(data2, 
    event.date = as.POSIXct(event.date, format = "%m/%d/%Y %H:%M:%S"), 
    date = as.Date(event.date, format = "%m/%d/%Y"),
    time = sub(".* ", "", event.date),
        stringsAsFactors = FALSE)

or, if we wanted them all as character then we could do this instead:

data3a <- transform(data2, 
    event.date = as.character(as.POSIXct(event.date, format = "%m/%d/%Y %H:%M:%S")), 
    date = as.character(as.Date(event.date, format = "%m/%d/%Y")),
    time = sub(".* ", "", event.date),
        stringsAsFactors = FALSE)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top