1

我需要从具有2012-09-27 07:05:59time形式的列的 data.frame 中分离时间和日期。然后我必须使用和列来提取特定日期/时间的数据。我该怎么做呢?可能是我想做这个的相反datetime

我尝试使用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

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

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

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

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

4

2 回答 2

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> 
于 2013-05-10T18:40:12.650 回答
1

我从评论中的理解是,你得到的数据实际上看起来像data2. 使用该原始数据,我们可以获得具有"POSIXct"类日期时间列、"Date"类日期列和"character"类时间列的 data3:

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)
于 2013-05-10T19:50:06.177 回答