首页 > 解决方案 > 计算从下午 5 点到次日下午 5 点的每日平均值

问题描述

我正在使用一个数据集,其中包含每 30 分钟测量一次的气象数据。我能够使用以下函数计算每日平均值(从 00:00 到 00:00):

daily_func_sum<-function(ts.in,date.ts){
  ts.in.xts<-xts(ts.in,date.ts)
  t<-period.apply(ts.in.xts, endpoints(ts.in.xts,"days"), sum)
  new.df<-data.frame(date=index(t), par_name=coredata(t))
  return(new.df)} 

我的数据看起来像这样:

           DateTime   Globalstrahlung Regen..30m Luft.Temperatur rel..Feuchte Luftdruck Windvelocity
1  2019-05-01 00:00:00       0          0            10.1         88.4       993          0.6
2  2019-05-01 00:30:00       0          0            10.4         81.5       993          0.5
3  2019-05-01 01:00:00       0          0             9.8         80.8       992          0.8
4  2019-05-01 01:30:00       0          0             9.2         82.2       993          0.4
5  2019-05-01 02:00:00       0          0             9.1         83.2       993          0.6
6  2019-05-01 02:30:00       0          0             6.8         97.4       993            0
7  2019-05-01 03:00:00       0          0             5.8         99.9       993            0
8  2019-05-01 03:30:00       0          0             6.6         96.9       992          0.4
9  2019-05-01 04:00:00       0          0             5.8         99.3       992          0.7
10 2019-05-01 04:30:00       3          0             5.1          100       993          0.5

我想根据从第一天下午 5 点到第二天下午 5 点的时间段计算每日平均值。例如从 2019.05.01 05:00 到 2019.05.02 5:00。到目前为止,我还没有找到将我想要的时间跨度纳入我的功能的方法。任何帮助将不胜感激。

编辑:这是乔治要求的数据。

> dput(WS_full_corr[1:50,])
structure(list(DateTime = structure(c(1556668800, 1556670600, 
1556672400, 1556674200, 1556676000, 1556677800, 1556679600, 1556681400, 
1556683200, 1556685000, 1556686800, 1556688600, 1556690400, 1556692200, 
1556694000, 1556695800, 1556697600, 1556699400, 1556701200, 1556703000, 
1556704800, 1556706600, 1556708400, 1556710200, 1556712000, 1556713800, 
1556715600, 1556717400, 1556719200, 1556721000, 1556722800, 1556724600, 
1556726400, 1556728200, 1556730000, 1556731800, 1556733600, 1556735400, 
1556737200, 1556739000, 1556740800, 1556742600, 1556744400, 1556746200, 
1556748000, 1556749800, 1556751600, 1556753400, 1556755200, 1556757000
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Timestamp = c(43586, 
43586.02083, 43586.04167, 43586.0625, 43586.08333, 43586.10417, 
43586.125, 43586.14583, 43586.16667, 43586.1875, 43586.20833, 
43586.22917, 43586.25, 43586.27083, 43586.29167, 43586.3125, 
43586.33333, 43586.35417, 43586.375, 43586.39583, 43586.41667, 
43586.4375, 43586.45833, 43586.47917, 43586.5, 43586.52083, 43586.54167, 
43586.5625, 43586.58333, 43586.60417, 43586.625, 43586.64583, 
43586.66667, 43586.6875, 43586.70833, 43586.72917, 43586.75, 
43586.77083, 43586.79167, 43586.8125, 43586.83333, 43586.85417, 
43586.875, 43586.89583, 43586.91667, 43586.9375, 43586.95833, 
43586.97917, 43587, 43587.02083), Time = structure(c(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, 2L, 3L), .Label = c("", "00:00:00", 
"00:30:00", "01:00:00", "01:30:00", "02:00:00", "02:30:00", "03:00:00", 
"03:30:00", "04:00:00", "04:30:00", "05:00:00", "05:30:00", "06:00:00", 
"06:30:00", "07:00:00", "07:30:00", "08:00:00", "08:30:00", "09:00:00", 
"09:30:00", "10:00:00", "10:30:00", "11:00:00", "11:30:00", "12:00:00", 
"12:30:00", "13:00:00", "13:30:00", "14:00:00", "14:30:00", "15:00:00", 
"15:30:00", "16:00:00", "16:30:00", "17:00:00", "17:30:00", "18:00:00", 
"18:30:00", "19:00:00", "19:30:00", "20:00:00", "20:30:00", "21:00:00", 
"21:30:00", "22:00:00", "22:30:00", "23:00:00", "23:30:00"), class = "factor"), 
    Date = c("2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", 
    "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-01", "2019-05-02", 
    "2019-05-02"), Power = c("11", "11", "11", "11", "11", "11", 
    "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", 
    "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", 
    "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", 
    "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", 
    "11", "11", "11", "11"), Cold.junction = c("10", "9.5", "8.9", 
    "8.4", "8", "7.6", "6.8", "5.9", "5.5", "5.2", "4.8", "4.6", 
    "4.9", "6.1", "8.8", "12.2", "14.6", "16", "16.8", "17.7", 
    "18.7", "19.8", "20.9", "21.9", "22.8", "23.7", "24.6", "25.3", 
    "25.8", "26.7", "25.4", "23.1", "22.6", "22.3", "22", "21.4", 
    "20.2", "18.6", "17.1", "16", "15.2", "14.2", "13.5", "13.3", 
    "13.2", "12.9", "12.6", "12.4", "11.9", "11.4"), Globalstrahlung = c("0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "3", "21", "51", 
    "94", "147", "341", "419", "493", "584", "664", "726", "771", 
    "827", "874", "898", "918", "907", "904", "884", "868", "574", 
    "668", "289", "437", "386", "311", "169", "50", "27", "6", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), Regen..30m = c("0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
    "0"), Luft.Temperatur = c("10.1", "10.4", "9.8", "9.2", "9.1", 
    "6.8", "5.8", "6.6", "5.8", "5.1", "4.6", "5", "6.3", "10.1", 
    "11.5", "12.4", "12.9", "13.4", "14.3", "14.7", "15.6", "16", 
    "16.9", "17.6", "17.6", "18.4", "18.6", "18.9", "19.4", "20.4", 
    "18.5", "19.2", "18.6", "18", "17.9", "17.4", "16.8", "15.6", 
    "14.6", "14.8", "13.7", "13.2", "13.5", "13.4", "12.8", "12.6", 
    "12.5", "12", "11.5", "11.2"), rel..Feuchte = c("88.4", "81.5", 
    "80.8", "82.2", "83.2", "97.4", "99.9", "96.9", "99.3", "100", 
    "100", "100", "100", "81.2", "73.6", "71.1", "66.2", "58.9", 
    "57.7", "51.8", "53.7", "52.2", "48.4", "46", "39", "40.3", 
    "37.6", "34.1", "36.1", "32.3", "43.7", "51.9", "53.8", "54.7", 
    "52", "55.6", "57.2", "64.5", "71.6", "67.2", "74.1", "79.5", 
    "75.3", "75.2", "80.8", "80.7", "79.7", "80.8", "82.8", "87"
    ), Luftdruck = c("993", "993", "992", "993", "993", "993", 
    "993", "992", "992", "993", "993", "993", "993", "993", "993", 
    "993", "993", "993", "993", "993", "993", "993", "993", "993", 
    "993", "993", "992", "992", "992", "992", "992", "991", "991", 
    "992", "992", "991", "992", "992", "992", "992", "992", "992", 
    "992", "993", "992", "992", "992", "992", "992", "992"), 
    Windvelocity = c("0.6", "0.5", "0.8", "0.4", "0.6", "0", 
    "0", "0.4", "0.7", "0.5", "0.7", "0.5", "0.4", "1", "1.7", 
    "2", "2.2", "3.2", "3.1", "2.9", "2.3", "2.8", "2.8", "2.6", 
    "2.6", "2.5", "2.3", "3.1", "2.6", "2.7", "2.9", "3.4", "3.7", 
    "3.6", "3.3", "2.9", "2.7", "2.1", "1.3", "2.1", "0.4", "0.5", 
    "1.3", "0.8", "0", "0.8", "1.3", "0.8", "0", "0"), Windrichtung = c("261.9", 
    "304.3", "279.1", "0", "292.5", "0", "0", "247.8", "82.9", 
    "73.8", "86.7", "79.5", "67.1", "283.1", "273.8", "281.6", 
    "280.6", "280.6", "276", "261.2", "269.5", "258.2", "271.5", 
    "255.5", "262.2", "241.2", "260.8", "253.7", "250.4", "261.1", 
    "268.6", "253.8", "265.3", "252", "262.7", "254", "262.7", 
    "269.5", "239.6", "276.8", "272", "244", "263.1", "247.8", 
    "0", "238.9", "238", "241.8", "0", "0"), Globalstrahlung.Avg = c("0.1", 
    "0.1", "0.1", "0", "0.1", "0.1", "0.1", "0", "0", "0.8", 
    "10.1", "37.7", "74.7", "112.2", "277.5", "389.5", "453.7", 
    "540.5", "625.7", "695.9", "748.2", "801.1", "855.3", "889.5", 
    "910.7", "912.5", "903.1", "885.2", "855.7", "879.1", "339.1", 
    "317.2", "408.6", "395", "333.7", "239.9", "118.3", "37.8", 
    "14.6", "1.8", "0.1", "0.1", "0.1", "0.1", "0.1", "0.1", 
    "0.1", "0.1", "0.1", "0.1"), Globalstrahlung.Max = c("0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "3", "21", "51", 
    "94", "160", "341", "419", "493", "584", "664", "726", "771", 
    "830", "875", "898", "919", "920", "916", "899", "897", "962", 
    "889", "708", "577", "458", "405", "308", "190", "52", "26", 
    "5", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), Globalstrahlung.Min = c("0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "3", "23", "53", 
    "87", "132", "348", "417", "497", "585", "665", "726", "770", 
    "827", "875", "897", "905", "890", "875", "753", "294", "172", 
    "111", "216", "258", "278", "147", "49", "27", "6", "0", 
    "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), Windvelocity.Avg = c("0.2", 
    "0.8", "0.4", "0.5", "0.7", "0.3", "0.3", "0", "0.3", "0.7", 
    "0.5", "0.5", "0.5", "0.3", "1.5", "2", "1.9", "2.9", "3.2", 
    "3", "2.7", "2.6", "2.7", "2.7", "2.7", "2.6", "2.6", "2.6", 
    "2.9", "2.9", "2.9", "2.9", "3.7", "4", "3.2", "3", "2.9", 
    "2.5", "1.6", "1.9", "1.1", "0.6", "1", "1.3", "0.6", "0.4", 
    "1", "0.9", "0.4", "0"), Windvelocity.Max = c("0.6", "1.2", 
    "0.8", "0.8", "0.8", "0.6", "0.6", "0.4", "0.7", "0.8", "0.7", 
    "0.7", "0.6", "1", "1.8", "2.2", "2.4", "3.5", "3.9", "3.3", 
    "3.1", "2.9", "2.9", "2.9", "2.8", "2.9", "3.2", "3.1", "3.3", 
    "3.3", "4", "3.4", "4.2", "4.3", "3.5", "3.4", "3.2", "2.8", 
    "2.1", "2.2", "1.9", "0.8", "1.3", "1.7", "1", "0.8", "1.3", 
    "1.3", "0.8", "0.3"), Windvelocity.Min = c("0", "0.5", "0", 
    "0.3", "0.4", "0", "0", "0", "0", "0.5", "0.3", "0.4", "0.4", 
    "0", "0.9", "1.7", "1.6", "2.4", "2.5", "2.8", "2.3", "2.3", 
    "2.4", "2.4", "2.5", "2.4", "2.1", "2.2", "2.6", "2.6", "2.4", 
    "2.7", "3.4", "3.5", "3", "2.5", "2.4", "2.1", "1.2", "1.2", 
    "0.4", "0.4", "0.6", "0.8", "0", "0", "0.7", "0.7", "0", 
    "0")), row.names = c(NA, 50L), class = "data.frame")

以前我删除了 Date、Time、Power 和 Cold.junction 列,因为它们与我的分析无关。

标签: r

解决方案


所以,我不完全了解您的数据集,但也许这会有所帮助:

library(tidyverse) # several packages that help
library(lubridate) # package for dealing with dates and times



# we take the dataframe
df %>%
    # ...create an additional column that is the actual time minus 5 hours and 
    # floor it to the "day" - i.e. same value if between 5:00 and 4:59 next day.
    mutate(adjusted_datetime = floor_date(DateTime - hours(5), unit = "day")) %>%
    # and now for each of those days...
    group_by(adjusted_datetime) %>%
    # we calculate the mean
    summarise(mean_globalstrahlung = mean(Globalstrahlung, na.rm = TRUE))

推荐阅读