2014-02-23 61 views
0

我得到的時間序列,如下圖所示:怎麼辦統計與時間日期

2013-12-27 00:31:15 
2013-12-29 17:01:17 
2013-12-31 01:52:41 
.... 

我的目標是要知道什麼時間在一天更重要的是,像大多數時間是在17期:00〜19:00。

爲了做到這一點,我認爲我應該畫每一個時間點作爲x軸的一個點,x軸的單位是分鐘。

  1. 我不知道如何完全使用R和ggplot2
  2. 我是否正確?我的意思是,有沒有更好的方法來獲得我的目標?

回答

2
library(chron) 

# create some test data - hrs 

set.seed(123) 

Lines <- "2013-12-27 00:31:15 
2013-12-29 17:01:17 
2013-12-31 01:52:41 
" 
tt0 <- times(read.table(text = Lines)[[2]]) %% 1 
rng <- range(tt0) 
hrs <- 24 * as.vector(sort(diff(rng) * runif(100)^2 + rng[1])) 

# create density, find maximum of it and plot 

d <- density(hrs) 
max.hrs <- d$x[which.max(d$y)] 
ggplot(data.frame(hrs)) + 
    geom_density(aes(hrs)) + 
    geom_vline(xintercept = max.hrs) 

,並提供:

> max.hrs # in hours - nearly 2 am 
[1] 1.989523 
> times(max.hrs/24) # convert to times 
[1] 01:59:22 

enter image description here

+0

非常感謝,我來試試 – NOrder