Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中分析时间_R - Fatal编程技术网

如何在R中分析时间

如何在R中分析时间,r,R,几个月来,我一直记下每天早上醒来的时间。我现在拥有的是一个包含24小时格式的时间的数据库,例如,2014-11-29 05:23:00,我可以将其精简为类似04:23 我想画出我起床时间的分布图。x轴是一天中的时间,y轴是频率。所有这些都非常简单,除了: 我现在头疼的是如何处理x轴刻度。因为有60分钟到一小时,我可以: 创建一个一天中分钟的刻度,其中时间04:23将转换为263分钟。这对我的计算很容易,但阅读起来不直观。当然,我可以轻松地将这些时间转换回去 用一百分钟的时间。因为在我的绘图中,轴

几个月来,我一直记下每天早上醒来的时间。我现在拥有的是一个包含24小时格式的时间的数据库,例如,
2014-11-29 05:23:00
,我可以将其精简为类似
04:23

我想画出我起床时间的分布图。x轴是一天中的时间,y轴是频率。所有这些都非常简单,除了:

我现在头疼的是如何处理x轴刻度。因为有60分钟到一小时,我可以:

  • 创建一个一天中分钟的刻度,其中时间04:23将转换为263分钟。这对我的计算很容易,但阅读起来不直观。当然,我可以轻松地将这些时间转换回去

  • 用一百分钟的时间。因为在我的绘图中,轴只会每一个小时被标记一次,这将很容易计算和阅读。但如果我想在60分钟内看到平均值或其他计算数据,我必须重新转换它,这可能会导致不准确。但我想这些都是次要的

  • 让R处理时间

  • 因为我唯一不知道怎么做的是第三种选择,我的问题是:

    如何在R中使用时间作为数据?最好的方法是什么


    下面是一个时间向量示例,如果您想尝试以下内容:

    t <- c("00:13:00", "00:30:00", "00:36:00", "00:45:00", "00:48:00", "01:08:00", "01:14:00", "01:15:00", "01:25:00", "02:06:00", "02:07:00", "02:22:00", "02:23:00", "02:36:00", "02:37:00", "02:55:00", "03:08:00", "03:10:00", "03:11:00", "03:13:00", "03:15:00", "03:23:00", "03:35:00", "03:55:00", "03:57:00", "03:58:00", "04:03:00", "04:06:00", "04:15:00", "04:21:00", "04:21:00", "04:22:00", "04:43:00", "04:48:00", "04:51:00", "04:58:00", "05:00:00", "05:02:00", "05:03:00", "05:17:00", "05:25:00", "05:34:00", "05:38:00", "05:45:00", "05:46:00", "05:50:00", "05:52:00", "06:10:00", "06:11:00", "06:13:00", "06:23:00", "06:26:00", "22:18:00", "23:27:00", "23:40:00", "23:53:00", "23:54:00", "23:58:00")
    
    套餐预测将帮助您

    library(ggplot2)
    
    #generate random times (between 4AM and 7:59AM) as a proxy for your data
    Random_times=c(); 
    for(i in 1:600){
      Random_times=c(Random_times,as.POSIXct(strptime(paste(sample(4:7,1),":",sample(0:59,1),":","00",sep=""),"%H:%M")))
    }
    
    #as absolute times
    P_random_times=as.POSIXct(Random_times, origin="1970-01-01")
    qplot(P_random_times)+xlim(c(strptime("03:00","%H:%M"),strptime("10:00","%H:%M")))
    
    
    
     #Or as mins from the minumum wake time 
    P_times=difftime(P_random_times, min(P_random_times),units="mins")
    qplot(as.numeric(P_times))
    

    您是否考虑过使用任意的“零点”?它可能是某个最小值或平均唤醒时间。我可以想象,您感兴趣的是时间之间的差异,因此“零”可以是任意时间点作为比较的锚。

    首先,您可能希望在这些文本字符串上使用R中的
    ?strtime
    (参见我的示例)。第二,ggplot2是你的朋友。如果你还没有使用它,这可以帮助你:你能举个例子吗?我不熟悉手册中的大部分术语。
    library(ggplot2)
    
    #generate random times (between 4AM and 7:59AM) as a proxy for your data
    Random_times=c(); 
    for(i in 1:600){
      Random_times=c(Random_times,as.POSIXct(strptime(paste(sample(4:7,1),":",sample(0:59,1),":","00",sep=""),"%H:%M")))
    }
    
    #as absolute times
    P_random_times=as.POSIXct(Random_times, origin="1970-01-01")
    qplot(P_random_times)+xlim(c(strptime("03:00","%H:%M"),strptime("10:00","%H:%M")))
    
    
    
     #Or as mins from the minumum wake time 
    P_times=difftime(P_random_times, min(P_random_times),units="mins")
    qplot(as.numeric(P_times))