一天中时间的直方图或密度图 [英] Histograms or Density plots of time of day

查看:139
本文介绍了一天中时间的直方图或密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有data.frame,其中的每一行都是带有开始和结束时间戳记的情节。

I have the data.frame in which every row is an episode with a start and an end timestamp.

test.DF<-dput(head(test.DF, n=50))
structure(list(start = structure(c(1189494920, 1189495400, 1189496120, 
1189496840, 1189497440, 1189498040, 1189498640, 1189501760, 1189503560, 
1190453600, 1247458520, 1247480840, 1247482880, 1247483840, 1247485040, 
1247486600, 1247487320, 1247488040, 1247488760, 1247490920, 1247491280, 
1247492480, 1247493680, 1247502440, 1247503160, 1247503520, 1247548040, 
1247549360, 1247550680, 1247552600, 1247553920, 1247557400, 1247558000, 
1247558480, 1247559440, 1247560400, 1247563760, 1247564960, 1247566640, 
1247567120, 1194935549, 1194936029, 1195722629, 1195724309, 1199691029, 
1199692349, 1202560229, 1208063669, 1208322989, 1188188112), class = c("POSIXct", 
"POSIXt"), tzone = ""), end = structure(c(1189495280, 1189495520, 
1189496360, 1189497080, 1189497560, 1189498160, 1189498760, 1189501880, 
1189503920, 1190453720, 1247458640, 1247480960, 1247483480, 1247484080, 
1247485640, 1247486840, 1247487560, 1247488640, 1247490440, 1247491160, 
1247491520, 1247492600, 1247493920, 1247502680, 1247503400, 1247504120, 
1247549240, 1247550560, 1247551280, 1247552720, 1247554400, 1247557880, 
1247558240, 1247559080, 1247559560, 1247560760, 1247563880, 1247565080, 
1247566760, 1247567240, 1194935669, 1194936269, 1195722749, 1195724429, 
1199691269, 1199692469, 1202560349, 1208063789, 1208323109, 1188204792
), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("start", 
"end"), row.names = c(NA, 50L), class = "data.frame")

我想看看这些情节在24小时周期内的分布情况。这可以是直方图,也可以是密度图,x轴为24H日循环。这可能吗?我想忽略情节的日期。

I would like to see the distribution of these episodes within a 24 hour cycle. That is either a histogram or a density plot, with the 24H day cycle in the x axis. Is this possible? I would like to ignore the dates of the episodes.

推荐答案

通过转换为 POSIXlt format,您可以轻松提取时间中的小时:

By converting to a POSIXltformat, you can easily extract the hour of the time:

par(mar=c(6,4,1,1))
Hour <- as.POSIXlt(test.DF$start)$hour
hist(Hour, breaks=seq(0, 23), main="Start time (hour)")

fun <- function(start.time, end.time){
    seq.POSIXt(
        as.POSIXlt(
            paste0("2000-01-01 ", as.POSIXlt(start.time)$hour, ":", as.POSIXlt(start.time)$min)
        ),
        as.POSIXlt(
            paste0("2000-01-01 ", as.POSIXlt(end.time)$hour, ":", as.POSIXlt(end.time)$min)
        ),
        by="min"
    )
}


HM <- vector(mode="list", dim(test.DF)[1])
for(i in seq(HM)){
    HM[[i]] <- fun(test.DF$start[i], test.DF$end[i])
}

HM2 <- as.POSIXlt(unlist(HM), origin="1970-01-01")

Hour <- HM2$hour 
hist(Hour, breaks=seq(0, 23))

HourMinute <- HM2$hour + HM2$min/60
hist(HourMinute, breaks=seq(0, 23, by=1/60))

这篇关于一天中时间的直方图或密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆