R在最短和最长时间获得价值

时间:2015-05-12 15:13:19

标签: r data.table

我有一些我需要轻松分析的数据。我想创建一周中每天平均使用量的图表。数据位于data.table,结构如下:

               time       value
2014-10-22 23:59:54   7433033.0
2014-10-23 00:00:12   7433034.0
2014-10-23 00:00:31   7433035.0
2014-10-23 00:00:49   7433036.0
...
2014-10-23 23:59:21   7443032.0
2014-10-23 23:59:40   7443033.0
2014-10-23 23:59:59   7443034.0
2014-10-24 00:00:19   7443035.0

由于该值是累积的,我需要一天的最大值,减去当天的最小值,然后将所有值平均为相同的天数。

我已经知道如何获取星期几(使用as.POSIXlt$wday)。那么如何才能获得日常差异呢?一旦我将数据放在如下结构中:

dayOfWeek  value
        0     10
        1     20
        2     50

我应该能够使用某些功能找到mean

以下是一个示例:

library(data.table)
data <- fread("http://pastebin.com/raw.php?i=GXGiCAiu", header=T)
#get the difference per day

#create average per day of week

3 个答案:

答案 0 :(得分:4)

使用R可以通过多种方式执行此操作。您可以使用基础R或avedata.table包中的dplyr。这些解决方案都将摘要添加为数据列。

数据

df <- data.frame(dayOfWeek = c(0L, 0L, 1L, 1L, 2L), 
                 value = c(10L, 5L, 20L, 60L, 50L))

base r

df$min <- ave(df$value, df$dayOfWeek, FUN = min)
df$max <- ave(df$value, df$dayOfWeek, FUN = max)

data.table

require(data.table)
setDT(df)[, ":="(min = min(value), max = max(value)), by = dayOfWeek][]

dplyr

require(dplyr)
df %>% group_by(dayOfWeek) %>% mutate(min = min(value), max = max(value))

如果您只想要摘要,还可以使用以下内容:

# base
aggregate(value~dayOfWeek, df, FUN = min)
aggregate(value~dayOfWeek, df, FUN = max)
# data.table
setDT(df)[, list(min = min(value), max = max(value)), by = dayOfWeek]
# dplyr
df %>% group_by(dayOfWeek) %>% summarise(min(value), max(value))

答案 1 :(得分:1)

这实际上比初看起来更棘手。我认为您需要两个单独的聚合,一个用于通过获取范围的差异来聚合每个日历日内的累积使用值,然后按秒计算每个日历日使用值的工作日。您可以使用weekdays()提取工作日,使用diff()上的range()计算每日差异,使用mean()计算均值,并使用aggregate()进行汇总:< / p>

set.seed(1);
N <- as.integer(60*60*24/19*14);
df <- data.frame(time=seq(as.POSIXct('2014-10-23 00:00:12',tz='UTC'),by=19,length.out=N)+rnorm(N,0,0.5), value=seq(7433034,by=1,length.out=N)+rnorm(N,0,0.5) );
head(df);
##                  time   value
## 1 2014-10-23 00:00:11 7433034
## 2 2014-10-23 00:00:31 7433035
## 3 2014-10-23 00:00:49 7433036
## 4 2014-10-23 00:01:09 7433037
## 5 2014-10-23 00:01:28 7433039
## 6 2014-10-23 00:01:46 7433039
tail(df);
##                      time   value
## 63658 2014-11-05 23:58:14 7496691
## 63659 2014-11-05 23:58:33 7496692
## 63660 2014-11-05 23:58:51 7496693
## 63661 2014-11-05 23:59:11 7496694
## 63662 2014-11-05 23:59:31 7496695
## 63663 2014-11-05 23:59:49 7496697
df2 <- aggregate(value~date,cbind(df,date=as.Date(df$time)),function(x) diff(range(x)));
df2;
##          date    value
## 1  2014-10-23 4547.581
## 2  2014-10-24 4546.679
## 3  2014-10-25 4546.410
## 4  2014-10-26 4545.726
## 5  2014-10-27 4546.602
## 6  2014-10-28 4545.194
## 7  2014-10-29 4546.136
## 8  2014-10-30 4546.454
## 9  2014-10-31 4545.712
## 10 2014-11-01 4546.901
## 11 2014-11-02 4544.684
## 12 2014-11-03 4546.378
## 13 2014-11-04 4547.061
## 14 2014-11-05 4547.082
df3 <- aggregate(value~dayOfWeek,cbind(df2,dayOfWeek=weekdays(df2$date)),mean);
df3;
##   dayOfWeek    value
## 1    Friday 4546.196
## 2    Monday 4546.490
## 3  Saturday 4546.656
## 4    Sunday 4545.205
## 5  Thursday 4547.018
## 6   Tuesday 4546.128
## 7 Wednesday 4546.609

答案 2 :(得分:1)

遇到这个寻找别的东西。我认为你正在寻找每周一,周二等的差异和平均值。坚持使用data.table允许快速一次性调用以获得每周的平均值和一周中每天的差异。这给出了7行和3列的输出。

library(data.table)
data <- fread("http://pastebin.com/raw.php?i=GXGiCAiu", header=T)

data_summary <- data[,list(mean = mean(value),
                           diff = max(value)-min(value)),
                     by = list(date = format(as.POSIXct(time), format = "%A"))]

这给出了7行和3列的输出。

        date    mean   diff
1:  Thursday 7470107 166966
2:    Friday 7445945   6119
3:  Saturday 7550000 100000
4:    Sunday 7550000 100000
5:    Monday 7550000 100000
6:   Tuesday 7550000 100000
7: Wednesday 7550000 100000