Reputation: 5141
I have a daily data set with temperature data and want to extract seasonal mean values. From daily data it is easy to subset and extract i.e. winter (December to February) values with
my.data.winter<-subset(my.data, format.Date(fecha, "%m")=="12" | format.Date(fecha, "%m")=="01" | format.Date(fecha, "%m")=="02")
giving
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
))
Now I would like to get the mean value for winter season for every distinct year in the series. A first attempt could be
DATE1<-"1982-12-01"
DATE2<-"1983-02-28"
my.newdata<-subset(my.data.winter, my.data.winter$fecha > DATE1 & my.data.winter$fecha < DATE2 )
To use this solution I need to build a date vector or data frame containing pairs of values for DATE1/DATE2 but I would prefer to set just the start value and select three months on. There has to be a cleaner/smarter way. Have checked lubridate
(when getting month number from December 1982 and adding 2 months I get "14" when I would like to get February 1983) and dplyr
but I can't figure out how this can be done.
Thanks in advance for your help
Upvotes: 1
Views: 1360
Reputation: 2290
Or you can use 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))
which gives you :
> x
Source: local data frame [4 x 3]
Groups: season [?]
season year2 mean
<chr> <dbl> <dbl>
1 winter 1982 290.7605
2 winter 1983 290.5127
3 winter 1984 290.9434
4 winter 1985 293.5770
NOTE: you can use this on your entire data set and it will do all of the subsetting for you rather than having to create four separate subsets of your primary data frame
Upvotes: 2