Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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,我是R的新手,我想画一些关于用电量的图表。我有两个大文件,包含一个时间戳和一个(累积)度量。我想要三种类型的图表: 每天每小时使用量 每周每天使用量 每年每月使用量 要做到(例如)每天每小时的使用量,我需要: 充分利用每天的每一小时 平均每天每小时 我掌握的资料如下: 2014-10-22 21:07:03+00:00,7432442.0 2014-10-22 21:07:21+00:00,7432443.0 2014-10-22 21:07:39+00:00,7432444.0 201

我是R的新手,我想画一些关于用电量的图表。我有两个大文件,包含一个时间戳和一个(累积)度量。我想要三种类型的图表:

  • 每天每小时使用量
  • 每周每天使用量
  • 每年每月使用量
要做到(例如)每天每小时的使用量,我需要:

  • 充分利用每天的每一小时
  • 平均每天每小时
我掌握的资料如下:

2014-10-22 21:07:03+00:00,7432442.0
2014-10-22 21:07:21+00:00,7432443.0
2014-10-22 21:07:39+00:00,7432444.0
2014-10-22 21:07:57+00:00,7432445.0
2014-10-22 21:08:15+00:00,7432446.0
2014-10-22 21:08:33+00:00,7432447.0
2014-10-22 21:08:52+00:00,7432448.0
2014-10-22 21:09:10+00:00,7432449.0
2014-10-22 21:09:28+00:00,7432450.0
我能够加载数据并使用
as.POSIXct()
解析时间戳。我也知道
diff()
是为了得到不同。但是如何将其应用于整个数据表

另外,在时间间隔内,数据会发生什么变化?例如:

2014-10-22 23:59:54+00:00,7433033.0
2014-10-23 00:00:12+00:00,7433034.0

在22日和23日之间使用了1个单位的东西。这应该被丢弃,还是添加到一个或另一个?

使用虚拟数据集

# sorted dataset
n = 1000
set.seed(1)
data = data.frame(
  time=seq(as.POSIXct('2014-10-22 21:07:00'),
           as.POSIXct('2014-10-23 10:07:00'),
           length.out=n),
  value=cumsum(runif(n))/n)
我们可以使用

# by hour of a day
starttime = as.POSIXct(format(min(data$time),'%Y-%m-%d'))
endtime = as.POSIXct(format(max(data$time),'%Y-%m-%d'))+60*60*24
hod = with(data,
           c(by(value,
              cut(time,seq(starttime,endtime,'hour')),
              function(x)x[length(x)]-x[1])))
然后通过这样做绘制结果

# plot
plot(as.POSIXct(names(hod)), hod, type='l')
barplot(hod)

编辑

跨天聚合可以通过

# average across days of the total usage within each hour
hod_m = c(by(hod,
             format(as.POSIXct(names(hod)),'%H'),
             mean,na.rm=T))
您不必计算每天(小时)内的总使用量,而可以计算每天(小时)内每次观察的“平均”使用量

# average usage per observation within each hour of each day
au = with(data,
           c(by(value,
                cut(time,seq(starttime,endtime,'hour')),
                function(x)(x[length(x)]-x[1])/(length(x)-1))))
# average across days of the above average usage within each day--hour
au_m = c(by(au,
            format(as.POSIXct(names(hod)),'%H'),
            mean,na.rm=T))
后一种衡量每次观察平均使用率的方法可以通过使用

# average usage per observation within each hour of each day
au = with(data,
           c(by(value,
                cut(time,seq(starttime,endtime,'hour')),
                function(x)(x[length(x)]-x[1])/(length(x)-1))))
# average across days of the above average usage within each day--hour
au_m = c(by(au,
            format(as.POSIXct(names(hod)),'%H'),
            mean,na.rm=T))

类似地,除了
mean
之外的聚合函数也可以在上面使用。

我建议您查看
?cut.POSIXt
?seq.POSIXt
,了解将数据分组到时间段的方法。如果问题是diff:data(mtcars)mtcars$diff=c(0,diff(mtcars$mpg)),我很抱歉;第二栏是什么?当你说你想绘制用法时,这就是第二列所表示的吗?非常好!获取所有时间的平均值怎么样,这样每天1:00到1:59之间的所有值都是平均值?您可以尝试
c(通过(hod,format(as.POSIXct(names(hod)),'%H'),mean,na.rm=T))