提取POSIXct小时和秒中的R绘图目的(extract hours and seconds fro

2019-06-24 16:09发布

假设我有以下data.frame foo

           start.time duration
1 2012-02-06 15:47:00      1
2 2012-02-06 15:02:00      2
3 2012-02-22 10:08:00      3
4 2012-02-22 09:32:00      4
5 2012-03-21 13:47:00      5

class(foo$start.time)回报

[1] "POSIXct" "POSIXt" 

我想创建的情节foo$durationfoo$start.time 。 在我的方案,我只是在一天的时间而不是一年的实际日感兴趣。 一个人如何去提取一天小时的时间:秒内从POSIXct类载体?

Answer 1:

这是一个很好的问题,并重点介绍了一些与日期的R.处理的lubridate包的难度是非常方便的,所以下面我将介绍两种方法,一是使用基地(由@ RJ-的建议),另一个使用lubridate。

重新在原岗位数据框(的前两行):

foo <- data.frame(start.time = c("2012-02-06 15:47:00", 
                                 "2012-02-06 15:02:00",
                                 "2012-02-22 10:08:00"),
                  duration   = c(1,2,3))

转换为POSIXct和POSIXt类(两种方法可以做到这一点)

# using base::strptime
t.str <- strptime(foo$start.time, "%Y-%m-%d %H:%M:%S")

# using lubridate::ymd_hms
library(lubridate)
t.lub <- ymd_hms(foo$start.time)

现在,提取时间为十进制小时

# using base::format
h.str <- as.numeric(format(t.str, "%H")) +
               as.numeric(format(t.str, "%M"))/60

# using lubridate::hour and lubridate::minute
h.lub <- hour(t.lub) + minute(t.lub)/60

证明这些方法是平等的:

identical(h.str, h.lub)

然后选择上述途径之一小数小时分配给foo$hr

foo$hr <- h.str

# If you prefer, the choice can be made at random:
foo$hr <- if(runif(1) > 0.5){ h.str } else { h.lub }

然后绘制使用GGPLOT2包:

library(ggplot2)
qplot(foo$hr, foo$duration) + 
             scale_x_datetime(labels = "%S:00")


Answer 2:

你可以依靠基础R:

# Using R 2.14.2
# The same toy data
foo <- data.frame(start.time = c("2012-02-06 15:47:00", 
                                 "2012-02-06 15:02:00",
                                 "2012-02-22 10:08:00"),
                  duration   = c(1,2,3))

由于类POSIXct包含以结构化方式的日期时间信息,你可以依靠substr在POSIXct矢量内的时间位置提取字符。 也就是说,给你知道你的POSIXct(打印时它会被显示),可以提取小时和分钟的格式:

# Extract hour and minute as a character vector, of the form "%H:%M"
substr(foo$start.time, 12, 16)

然后将其粘贴到任意日期将其转换回POSIXct。 在这个例子中我先用2012年1月,但如果你没有指定一个日期,而使用format R使用当前的日期。

# Store time information as POSIXct, using an arbitrary date
foo$time <- as.POSIXct(paste("2012-01-01", substr(foo$start.time, 12, 16)))

而且两者plotggplot2知道如何格式化POSIXct次开箱。

# Plot it using base graphics
plot(duration~time, data=foo)

# Plot it using ggplot2 (0.9.2.1)
library(ggplot2)
qplot(x=time, y=duration, data=foo)


Answer 3:

此代码是远远快于转换为字符串并返回到数字

time <- c("1979-11-13T08:37:19-0500", "2014-05-13T08:37:19-0400");
time.posix <- as.POSIXct(time, format = "%Y-%m-%dT%H:%M:%S%z");
time.epoch <- as.vector(unclass(time.posix));
time.poslt <- as.POSIXlt(time.posix, tz = "America/New_York");
time.hour.new.york <- time.poslt$hour + time.poslt$min/60 + time.poslt$sec/3600;

> time;
[1] "1979-11-13T08:37:19-0500" "2014-05-13T08:37:19-0400"
> time.posix;
[1] "1979-11-13 15:37:19 IST" "2014-05-13 15:37:19 IDT"
> time.poslt;
[1] "1979-11-13 08:37:19 EST" "2014-05-13 08:37:19 EDT"
> time.epoch;
[1]  311348239 1399984639
> time.hour.new.york;
[1] 8.621944 8.621944


Answer 4:

Lubridate不处理一天的数据的时间,因此哈德利建议HMS包这种类型的数据。 像这样的东西会工作:

library(lubridate)
foo <- data.frame(start.time = parse_datetime(c("2012-02-06 15:47:00", 
                                 "2012-02-06 15:02:00",
                                 "2012-02-22 10:08:00")),
                  duration   = c(1,2,3))


foo<-foo %>% mutate(time_of_day=hms::hms(second(start.time),minute(start.time),hour(start.time)))

当心2个潜在的问题 - 1)lubridate具有不同的功能,称为HMS和2)HMS HMS ::采用的参数以相反的顺序来,暗示其名称(以便几秒钟可提供)



文章来源: extract hours and seconds from POSIXct for plotting purposes in R