R代码按组获取时间序列数据的最大计数 [英] R code to get max count of time series data by group
问题描述
我想获得时间序列数据的摘要,其中组是Flare",FlareLength 的最大值是该组感兴趣的数据.
如果我有一个数据框,就像这样:
<预><代码>日期耀斑耀斑长度1 2015-12-01 0 12 2015-12-02 0 23 2015-12-03 0 34 2015-12-04 0 45 2015-12-05 0 56 2015-12-06 0 67 2015-12-07 1 18 2015-12-08 1 29 2015-12-09 1 310 2015-12-10 1 411 2015-12-11 0 112 2015-12-12 0 213 2015-12-13 0 314 2015-12-14 0 415 2015-12-15 0 516 2015-12-16 0 617 2015-12-17 0 718 2015-12-18 0 819 2015-12-19 0 920 2015-12-20 0 1021 2015-12-21 0 1122 2016-01-11 1 123 2016-01-12 1 224 2016-01-13 1 325 2016-01-14 1 426 2016-01-15 1 527 2016-01-16 1 628 2016-01-17 1 729 2016-01-18 1 8我希望输出如下:
Date Flare FlareLength1 2015-12-06 0 62 2015-12-10 1 43 2015-12-21 0 114 2016-01-18 1 8
我尝试过各种聚合形式,但我对时间序列皱纹不是很熟悉.
使用 dplyr
,我们可以通过将 FlareLength
与之前的 进行比较来创建分组变量FlareLength
值并选择组中具有 maximum
FlareLength
的行.
库(dplyr)df%>%group_by(gr = cumsum(FlareLength < lag(FlareLength,默认值 = first(FlareLength)))) %>%切片(which.max(FlareLength))%>%取消分组()%>%选择(-gr)# 小费:4 x 3# 日期耀斑 FlareLength# <fct><int><int>#1 2015-12-06 0 6#2 2015-12-10 1 4#3 2015-12-21 0 11#4 2016-01-18 1 8
在带有 ave
的基础 R 中,我们可以执行与
subset(df, FlareLength == ave(FlareLength, cumsum(c(TRUE, diff(FlareLength) <0)),乐趣 = 最大))
I'd like to get a summary of time series data where group is "Flare" and the max value of the FlareLength is the data of interest for that group.
If I have a dataframe, like this:
Date Flare FlareLength
1 2015-12-01 0 1
2 2015-12-02 0 2
3 2015-12-03 0 3
4 2015-12-04 0 4
5 2015-12-05 0 5
6 2015-12-06 0 6
7 2015-12-07 1 1
8 2015-12-08 1 2
9 2015-12-09 1 3
10 2015-12-10 1 4
11 2015-12-11 0 1
12 2015-12-12 0 2
13 2015-12-13 0 3
14 2015-12-14 0 4
15 2015-12-15 0 5
16 2015-12-16 0 6
17 2015-12-17 0 7
18 2015-12-18 0 8
19 2015-12-19 0 9
20 2015-12-20 0 10
21 2015-12-21 0 11
22 2016-01-11 1 1
23 2016-01-12 1 2
24 2016-01-13 1 3
25 2016-01-14 1 4
26 2016-01-15 1 5
27 2016-01-16 1 6
28 2016-01-17 1 7
29 2016-01-18 1 8
I'd like output like:
Date Flare FlareLength
1 2015-12-06 0 6
2 2015-12-10 1 4
3 2015-12-21 0 11
4 2016-01-18 1 8
I have tried various aggregate forms but I'm not very familiar with the time series wrinkle.
Using dplyr
, we can create a grouping variable by comparing the FlareLength
with the previous FlareLength
value and select the row with maximum
FlareLength
in the group.
library(dplyr)
df %>%
group_by(gr = cumsum(FlareLength < lag(FlareLength,
default = first(FlareLength)))) %>%
slice(which.max(FlareLength)) %>%
ungroup() %>%
select(-gr)
# A tibble: 4 x 3
# Date Flare FlareLength
# <fct> <int> <int>
#1 2015-12-06 0 6
#2 2015-12-10 1 4
#3 2015-12-21 0 11
#4 2016-01-18 1 8
In base R with ave
we can do the same as
subset(df, FlareLength == ave(FlareLength, cumsum(c(TRUE, diff(FlareLength) < 0)),
FUN = max))
这篇关于R代码按组获取时间序列数据的最大计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!