用R选择三个月的时间序列

时间:2017-03-21 15:25:09

标签: r time-series dplyr lubridate

我有一个包含温度数据的每日数据集,想要提取季节性平均值。从日常数据中,很容易用

来分组和提取冬季(12月到2月)的值
$arr = json_decode('{"people":[
{
  "id": "8080",
  "content": "foo",
  "member": [123, 456],
  "interval": 7
},
{ 
  "id": "8097",
  "content": "bar",
  "member": [1234, 4567],
  "interval": 7
}


]}', true);

$results = array_filter($arr['people'], function($people) {
    for($i=0; $i<count($people['member']); $i++){
        return $people['member'][$i] == 123; 
    }
});

echo json_encode($results);

my.data.winter<-subset(my.data, format.Date(fecha, "%m")=="12" | format.Date(fecha, "%m")=="01" | format.Date(fecha, "%m")=="02")

现在我希望获得该系列中每个不同年份的冬季平均值。第一次尝试可能是

dput(my.data.winter)
structure(list(X = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L,
50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 335L, 336L,
337L, 338L, 339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L,
348L, 349L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L,
359L, 360L, 361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L,
370L, 371L, 372L, 373L, 374L, 375L, 376L, 377L, 378L, 379L, 380L,
381L, 382L, 383L, 384L, 385L, 386L, 387L, 388L, 389L, 390L, 391L,
392L, 393L, 394L, 395L, 396L, 397L, 398L, 399L, 400L, 401L, 402L,
403L, 404L, 405L, 406L, 407L, 408L, 409L, 410L, 411L, 412L, 413L,
414L, 415L, 416L, 417L, 418L, 419L, 420L, 421L, 422L, 423L, 424L,
700L, 701L, 702L, 703L, 704L, 705L, 706L, 707L, 708L, 709L, 710L,
711L, 712L, 713L, 714L, 715L, 716L, 717L, 718L, 719L, 720L, 721L,
722L, 723L, 724L, 725L, 726L, 727L, 728L, 729L, 730L, 731L, 732L,
733L, 734L, 735L, 736L, 737L, 738L, 739L, 740L, 741L, 742L, 743L,
744L, 745L, 746L, 747L, 748L, 749L, 750L, 751L, 752L, 753L, 754L,
755L, 756L, 757L, 758L, 759L, 760L, 761L, 762L, 763L, 764L, 765L,
766L, 767L, 768L, 769L, 770L, 771L, 772L, 773L, 774L, 775L, 776L,
777L, 778L, 779L, 780L, 781L, 782L, 783L, 784L, 785L, 786L, 787L,
788L, 789L, 790L, 1066L, 1067L, 1068L, 1069L, 1070L, 1071L, 1072L,
1073L, 1074L, 1075L), fecha = structure(1:250, .Label = c("1982-01-01",
"1982-01-02", "1982-01-03", "1982-01-04", "1982-01-05", "1982-01-06",
"1982-01-07", "1982-01-08", "1982-01-09", "1982-01-10", "1982-01-11",
"1982-01-12", "1982-01-13", "1982-01-14", "1982-01-15", "1982-01-16",
"1982-01-17", "1982-01-18", "1982-01-19", "1982-01-20", "1982-01-21",
"1982-01-22", "1982-01-23", "1982-01-24", "1982-01-25", "1982-01-26",
"1982-01-27", "1982-01-28", "1982-01-29", "1982-01-30", "1982-01-31",
"1982-02-01", "1982-02-02", "1982-02-03", "1982-02-04", "1982-02-05",
"1982-02-06", "1982-02-07", "1982-02-08", "1982-02-09", "1982-02-10",
"1982-02-11", "1982-02-12", "1982-02-13", "1982-02-14", "1982-02-15",
"1982-02-16", "1982-02-17", "1982-02-18", "1982-02-19", "1982-02-20",
"1982-02-21", "1982-02-22", "1982-02-23", "1982-02-24", "1982-02-25",
"1982-02-26", "1982-02-27", "1982-02-28", "1982-12-01", "1982-12-02",
"1982-12-03", "1982-12-04", "1982-12-05", "1982-12-06", "1982-12-07",
"1982-12-08", "1982-12-09", "1982-12-10", "1982-12-11", "1982-12-12",
"1982-12-13", "1982-12-14", "1982-12-15", "1982-12-16", "1982-12-17",
"1982-12-18", "1982-12-19", "1982-12-20", "1982-12-21", "1982-12-22",
"1982-12-23", "1982-12-24", "1982-12-25", "1982-12-26", "1982-12-27",
"1982-12-28", "1982-12-29", "1982-12-30", "1982-12-31", "1983-01-01",
"1983-01-02", "1983-01-03", "1983-01-04", "1983-01-05", "1983-01-06",
"1983-01-07", "1983-01-08", "1983-01-09", "1983-01-10", "1983-01-11",
"1983-01-12", "1983-01-13", "1983-01-14", "1983-01-15", "1983-01-16",
"1983-01-17", "1983-01-18", "1983-01-19", "1983-01-20", "1983-01-21",
"1983-01-22", "1983-01-23", "1983-01-24", "1983-01-25", "1983-01-26",
"1983-01-27", "1983-01-28", "1983-01-29", "1983-01-30", "1983-01-31",
"1983-02-01", "1983-02-02", "1983-02-03", "1983-02-04", "1983-02-05",
"1983-02-06", "1983-02-07", "1983-02-08", "1983-02-09", "1983-02-10",
"1983-02-11", "1983-02-12", "1983-02-13", "1983-02-14", "1983-02-15",
"1983-02-16", "1983-02-17", "1983-02-18", "1983-02-19", "1983-02-20",
"1983-02-21", "1983-02-22", "1983-02-23", "1983-02-24", "1983-02-25",
"1983-02-26", "1983-02-27", "1983-02-28", "1983-12-01", "1983-12-02",
"1983-12-03", "1983-12-04", "1983-12-05", "1983-12-06", "1983-12-07",
"1983-12-08", "1983-12-09", "1983-12-10", "1983-12-11", "1983-12-12",
"1983-12-13", "1983-12-14", "1983-12-15", "1983-12-16", "1983-12-17",
"1983-12-18", "1983-12-19", "1983-12-20", "1983-12-21", "1983-12-22",
"1983-12-23", "1983-12-24", "1983-12-25", "1983-12-26", "1983-12-27",
"1983-12-28", "1983-12-29", "1983-12-30", "1983-12-31", "1984-01-01",
"1984-01-02", "1984-01-03", "1984-01-04", "1984-01-05", "1984-01-06",
"1984-01-07", "1984-01-08", "1984-01-09", "1984-01-10", "1984-01-11",
"1984-01-12", "1984-01-13", "1984-01-14", "1984-01-15", "1984-01-16",
"1984-01-17", "1984-01-18", "1984-01-19", "1984-01-20", "1984-01-21",
"1984-01-22", "1984-01-23", "1984-01-24", "1984-01-25", "1984-01-26",
"1984-01-27", "1984-01-28", "1984-01-29", "1984-01-30", "1984-01-31",
"1984-02-01", "1984-02-02", "1984-02-03", "1984-02-04", "1984-02-05",
"1984-02-06", "1984-02-07", "1984-02-08", "1984-02-09", "1984-02-10",
"1984-02-11", "1984-02-12", "1984-02-13", "1984-02-14", "1984-02-15",
"1984-02-16", "1984-02-17", "1984-02-18", "1984-02-19", "1984-02-20",
"1984-02-21", "1984-02-22", "1984-02-23", "1984-02-24", "1984-02-25",
"1984-02-26", "1984-02-27", "1984-02-28", "1984-02-29", "1984-12-01",
"1984-12-02", "1984-12-03", "1984-12-04", "1984-12-05", "1984-12-06",
"1984-12-07", "1984-12-08", "1984-12-09", "1984-12-10"), class = "factor"),
    V1 = c(290.919993499294, 291.69999348186, 292.149993471801,
    291.859993478283, 291.969993475825, 292.339993467554, 292.289993468672,
    292.179993471131, 291.909993477166, 291.879993477836, 291.819993479177,
    292.74999345839, 292.679993459955, 291.909993477166, 291.489993486553,
    291.479993486777, 291.619993483648, 291.829993478954, 291.959993476048,
    291.229993492365, 291.049993496388, 290.949993498623, 290.949993498623,
    290.939993498847, 290.609993506223, 290.359993511811, 290.369993511587,
    290.369993511587, 290.729993503541, 290.459993509576, 290.409993510693,
    290.549993507564, 290.609993506223, 290.599993506446, 290.459993509576,
    290.459993509576, 290.149993516505, 290.129993516952, 290.149993516505,
    290.38999351114, 290.41999351047, 290.069993518293, 289.919993521646,
    290.009993519634, 289.999993519858, 289.759993525222, 289.799993524328,
    289.779993524775, 289.399993533269, 289.839993523434, 290.039993518963,
    289.669993527234, 289.149993538857, 290.139993516728, 289.999993519858,
    289.889993522316, 289.829993523657, 289.47999353148, 288.999993542209,
    293.28999344632, 293.359993444756, 293.209993448108, 293.099993450567,
    293.039993451908, 292.57999346219, 292.599993461743, 292.719993459061,
    292.799993457273, 292.839993456379, 292.739993458614, 292.769993457943,
    292.839993456379, 292.549993462861, 292.549993462861, 292.439993465319,
    292.159993471578, 290.979993497953, 291.889993477613, 291.369993489236,
    291.249993491918, 291.229993492365, 291.079993495718, 290.799993501976,
    289.949993520975, 290.029993519187, 290.899993499741, 291.199993493035,
    290.849993500859, 290.869993500412, 290.859993500635, 290.269993513823,
    290.209993515164, 290.399993510917, 290.519993508235, 290.569993507117,
    290.649993505329, 290.629993505776, 290.58999350667, 290.689993504435,
    290.75999350287, 290.769993502647, 290.769993502647, 290.609993506223,
    290.469993509352, 290.379993511364, 290.409993510693, 290.7899935022,
    290.359993511811, 289.399993533269, 289.349993534386, 289.539993530139,
    289.50999353081, 289.469993531704, 289.439993532375, 289.47999353148,
    289.47999353148, 289.519993530586, 289.409993533045, 289.219993537292,
    289.189993537962, 289.159993538633, 289.169993538409, 289.719993526116,
    289.659993527457, 289.629993528128, 289.579993529245, 289.289993535727,
    288.999993542209, 289.209993537515, 289.369993533939, 289.449993532151,
    289.449993532151, 289.049993541092, 288.929993543774, 289.13999353908,
    289.319993535057, 289.469993531704, 289.549993529916, 289.589993529022,
    289.629993528128, 289.529993530363, 289.469993531704, 289.389993533492,
    289.409993533045, 289.33999353461, 289.369993533939, 289.379993533716,
    289.589993529022, 289.579993529245, 294.749993413687, 294.289993423969,
    294.149993427098, 294.099993428215, 294.019993430004, 293.949993431568,
    293.849993433803, 293.779993435368, 293.739993436262, 293.629993438721,
    292.699993459508, 292.729993458837, 292.979993453249, 292.769993457943,
    292.549993462861, 292.469993464649, 292.509993463755, 292.559993462637,
    293.099993450567, 293.189993448555, 293.209993448108, 291.999993475154,
    291.849993478507, 291.799993479624, 291.989993475378, 292.269993469119,
    291.389993488789, 291.429993487895, 291.439993487671, 291.2999934908,
    291.339993489906, 291.109993495047, 291.059993496165, 291.039993496612,
    291.009993497282, 290.899993499741, 291.029993496835, 291.149993494153,
    291.179993493482, 291.269993491471, 291.359993489459, 291.339993489906,
    290.38999351114, 290.169993516058, 290.139993516728, 290.169993516058,
    290.309993512928, 290.699993504211, 290.519993508235, 290.509993508458,
    290.459993509576, 290.479993509129, 290.489993508905, 290.319993512705,
    290.24999351427, 290.189993515611, 290.189993515611, 290.229993514717,
    290.209993515164, 290.41999351047, 290.469993509352, 290.099993517622,
    289.389993533492, 290.029993519187, 290.299993513152, 290.329993512481,
    290.299993513152, 290.059993518516, 290.029993519187, 289.819993523881,
    288.729993548244, 288.819993546233, 289.109993539751, 289.109993539751,
    289.129993539304, 289.279993535951, 289.149993538857, 288.76999354735,
    288.899993544444, 289.059993540868, 289.099993539974, 289.129993539304,
    289.13999353908, 289.619993528351, 289.559993529692, 289.33999353461,
    288.999993542209, 288.519993552938, 288.449993554503, 288.419993555173,
    288.239993559197, 294.509993419051, 294.539993418381, 294.319993423298,
    293.279993446544, 293.259993446991, 293.179993448779, 293.099993450567,
    293.029993452132, 292.969993453473, 293.579993439838)), .Names = c("X",
"fecha", "V1"), class = "data.frame", row.names = c(NA, -250L
))

要使用此解决方案,我需要构建一个包含DATE1 / DATE2值对的日期向量或数据框,但我更愿意只设置起始值并选择三个月。必须有一个更清洁/更聪明的方式。已经检查了DATE1<-"1982-12-01" DATE2<-"1983-02-28" my.newdata<-subset(my.data.winter, my.data.winter$fecha > DATE1 & my.data.winter$fecha < DATE2 ) (从1982年12月获得月份数,并且我得到了2个月时间&#34; 14&#34;当我想要获得1983年2月时)和lubridate但我可以&#39;弄清楚如何做到这一点。

提前感谢您的帮助

1 个答案:

答案 0 :(得分:2)

或者您可以使用+-------+--------+--------------+---------+--------------+----------------+ | Id | UserId | Type | Amount | RunningTotal | desired_result | +-------+--------+--------------+---------+--------------+----------------+ | 2759 | 750 | charge | -50.00 | -50.00 | -50.00 | | 2760 | 750 | charge | -125.00 | -175.00 | -175.00 | | 4308 | 750 | paymentGC | 50.00 | -125.00 | -125.00 | | 4309 | 750 | paymentGC | 125.00 | 0.00 | 0.00 | | 19916 | 750 | charge | -1.00 | -1.00 | -1.00 | | 19917 | 750 | creditRefund | 124.00 | 123.00 | 123.00 | | 23238 | 750 | paymentCC | 50.00 | 50.00 | 50.00 | | 23239 | 750 | paymentCC | 125.00 | 175.00 | 175.00 | | 23240 | 750 | charge | -50.00 | 125.00 | 125.00 | | 23241 | 750 | charge | -125.00 | 0.00 | 0.00 | | 41300 | 750 | creditRefund | 125.00 | 125.00 | 125.00 | | 42054 | 750 | paymentCC | 50.00 | 50.00 | 50.00 | | 42055 | 750 | paymentCC | 125.00 | 175.00 | 175.00 | | 42056 | 750 | charge | -50.00 | 125.00 | 125.00 | | 42057 | 750 | charge | -125.00 | 0.00 | 0.00 | | 56983 | 750 | creditRefund | 125.00 | 125.00 | 125.00 | | 63083 | 750 | paymentCC | 50.00 | 50.00 | 50.00 | | 63084 | 750 | paymentCC | 125.00 | 175.00 | 175.00 | | 63085 | 750 | charge | -50.00 | 125.00 | 125.00 | | 63086 | 750 | charge | -125.00 | 0.00 | 0.00 | | 80829 | 750 | creditRefund | 125.00 | 125.00 | 125.00 | +-------+--------+--------------+---------+--------------+----------------+

dplyr

给你:

library(dplyr)
x <- my.data.winter %>%

  #add month and year column using lubridate package
  mutate(month = lubridate::month(fecha), year = lubridate::year(fecha))%>%

  #add the seasons for each value
  mutate(season = case_when(.$month == 12 | .$month == 1 | .$month == 2 ~ "winter",
                            .$month == 3 | .$month == 4 | .$month == 5 ~ "spring",
                            .$month == 6 | .$month == 7 | .$month == 8 ~ "summer",
                            .$month == 9 | .$month == 10 | .$month == 11 ~ "fall")) %>%

  #if month is december, classify it as the next year (for aggregation)
  mutate(year2 = ifelse(.$month == 12, year+1, year)) %>%
  group_by(season, year2) %>%
  summarize(mean = mean(V1))

注意:您可以在整个数据集中使用它,它将为您完成所有子集,而不必创建主数据框的四个独立子集