格式为YYYY-MM-DD的分割列HH:MM:SS到两个(日期对象)列YYYY-MM-DD和HH:MM:SS在R中

时间:2013-05-10 18:30:59

标签: r date datetime lubridate

我需要将时间和日期与 2012-09-27 07:05:59 形式的time列的data.frame隔离开来。 然后,我必须使用datetime列来提取特定日期/时间的数据。 我该怎么做呢?可能我想要反过来this one

我尝试使用strptime函数和lubridate包,但无法使其正常工作。

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")

以下内容为NA s:

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

我也无法成功使用lubridate ...

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

我对我应该使用哪种日期格式/包感到困惑。

2 个答案:

答案 0 :(得分:6)

你走了:

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> 

以下是您的列类型,显示我使用第一个命令将POSIXlt更改为POSIXct

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

$VE
[1] "integer"

$pt
[1] "POSIXct" "POSIXt" 

$date
[1] "Date"

$time
[1] "character"

R> 

答案 1 :(得分:1)

我对评论的理解是,您获得的数据实际上看起来像data2。使用原始数据,我们可以获得data3,其中包含"POSIXct"类datetime列,"Date"类日期列和"character"类时间列:

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)

或者,如果我们想要它们全部作为角色,那么我们可以这样做:

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)