特定时间窗口内的平均数据(跨越多天的POSIXct时间戳)

huangapple go评论69阅读模式
英文:

Average data between specific time window that spans across days (POSIXct)

问题

我要计算在任意一天的上午11点到第二天上午10点之间的23个小时内所有列的平均值。所以在上面的示例中,它将忽略行号1到10,然后计算行号11到33,然后34到56,依此类推...有些时间戳可能会丢失,所以我不能使用'每23行的平均值' - 它必须考虑特定的时间。我检查了这个[答案][1],但它不适用于我的情况,因为平均值必须跨越连续的日期,而不仅仅是时间。我还查看了这个[答案][2],但在这里也不适用,因为时间和日期都会变化。

请问有什么帮助吗?
以下是可重现数据的200行。

jnk2 <- structure(list(V1 = structure(c(1412125200, 1412128800, 1412132400, 
1412136000, 1412139600, 1412143200, 1412146800, 1412150400, 1412154000, 
1412157600, 1412161200, 1412164800, 1412168400, 1412172000, 1412175600, 
1412179200, 1412182800, 1412186400, 1412190000, 1412193600, 1412197200, 
1412200800, 1412204400, 1412211600, 1412215200, 1412218800, 1412222400, 
1412226000, 1412229600, 1412233200, 1412236800, 1412240400, 1412244000, 
1412247600, 1412251200, 1412254800, 1412258400, 1412262000, 1412265600, 
1412269200, 1412272800, 1412276400, 1412280000, 1412283600, 1412287200, 
1412290800, 1412298000, 1412301600, 1412305200, 1412308800, 1412312400, 
1412316000, 1412319600, 1412323200, 1412326800, 1412330400, 1412334000, 
1412337600, 1412341200, 1412344800, 1412348400, 1412352000, 1412355600, 
1412359200, 1412362800, 1412366400, 1412370000, 1412373600, 1412377200, 
1412384400, 1412388000, 1412391600, 1412395200, 1412398800, 1412402400, 
1412406000, 1412409600, 1412413200, 1412416800, 1412420400, 1412434800, 
1412438400, 1412442000, 1412445600, 1412449200, 1412452800, 1412456400, 
1412460000, 1412463600, 1412470800, 1412474400, 1412478000, 1412481600, 
1412485200, 1412488800, 1412492400, 1412496000, 1412499600, 1412503200, 
1412506800, 1412510400, 1412514000, 1412517600, 1412521200, 1412524800, 
1412528400, 1412532000, 1412535600, 1412539200, 1412542800, 1412546400, 
1412550000, 1412557200, 1412560800, 1412564400, 1412568000, 1412571600, 
1412575200, 1412578800, 1412582400, 1412600400, 1412604000, 1412618400, 
1412622000, 1412625600, 1412629200, 1412632800, 1412636400, 1412643600, 
1412647200, 1412650800, 1412654400, 1412661600, 1412665200, 1412668800, 
141267240

<details>
<summary>英文:</summary>

I have a dataframe that looks like this: 

                 V1  ZDR_1100  ZDR_1300   ZDR_500   ZDR_700   ZDR_900

1 2014-10-01 01:00:00 0.3061670 0.2915011 0.4471578 0.3707882 0.3220131
2 2014-10-01 02:00:00 0.3047930 0.2908829 0.4396977 0.3702261 0.3264016
3 2014-10-01 03:00:00 0.3673773 0.3465008 0.4248641 0.3738108 0.3612058
4 2014-10-01 04:00:00 0.7917090 0.7474398 0.7844732 0.7875300 0.7901948
5 2014-10-01 05:00:00 0.5127400 0.5292109 0.5635168 0.5036880 0.4987466
6 2014-10-01 06:00:00 0.4886693 0.4670046 0.5947554 0.5450330 0.5002735
7 2014-10-01 07:00:00 0.4279186 0.4064758 0.6278571 0.5214516 0.4599871
8 2014-10-01 08:00:00 0.4148058 0.4057495 0.4145295 0.3357730 0.3575914
9 2014-10-01 09:00:00 0.4484941 0.3364708 0.6065206 0.5753551 0.5444157
10 2014-10-01 10:00:00 0.3583347 0.2726725 0.8397633 0.7179004 0.5148551
11 2014-10-01 11:00:00 0.6288304 0.4348854 0.9070940 0.8604263 0.7807477
12 2014-10-01 12:00:00 0.8406678 0.5889769 1.0873081 1.0566411 0.9841537
13 2014-10-01 13:00:00 0.5776251 0.5769099 0.8850426 0.7576680 0.6303576
14 2014-10-01 14:00:00 0.4239585 0.4048328 0.7637417 0.6737831 0.5313644
15 2014-10-01 15:00:00 0.5041545 0.4462159 0.7451661 0.6600187 0.5695404
16 2014-10-01 16:00:00 0.4124360 0.3316415 0.5814130 0.5263740 0.4752262
17 2014-10-01 17:00:00 0.2888649 0.2678696 0.4819308 0.3841180 0.3152392
18 2014-10-01 18:00:00 0.2576780 0.2466471 0.4769507 0.3536859 0.2821441
19 2014-10-01 19:00:00 0.6220902 0.6045179 0.6700871 0.5979397 0.6061213
20 2014-10-01 20:00:00 1.2761947 1.3354990 1.0488132 1.1741320 1.2581224
21 2014-10-01 21:00:00 1.1374178 1.0125290 1.0566788 1.1957064 1.2787991
22 2014-10-01 22:00:00 1.0113306 0.8008907 1.0279799 1.1066179 1.1051438
23 2014-10-01 23:00:00 1.0169086 0.8378412 0.9953001 1.0655715 1.1336912
24 2014-10-02 01:00:00 0.9051049 0.8053902 1.1131751 1.1229773 1.0472362
25 2014-10-02 02:00:00 0.7473209 0.5441372 1.1298675 1.1511115 0.9912685
26 2014-10-02 03:00:00 0.9986012 0.7766588 1.1412711 1.2229866 1.1356994
27 2014-10-02 04:00:00 1.1669945 1.0475378 1.2551267 1.3402538 1.3045392
28 2014-10-02 05:00:00 1.1271080 0.8865925 1.2165770 1.3225403 1.3053887
29 2014-10-02 06:00:00 0.7321415 0.6889920 0.7803240 0.7641697 0.7498886
30 2014-10-02 07:00:00 0.4115109 0.3451894 0.6870765 0.6070242 0.5015284
31 2014-10-02 08:00:00 0.2850142 0.2842648 0.5309662 0.3949138 0.3142576
32 2014-10-02 09:00:00 0.3175762 0.3087539 0.5255503 0.4005035 0.3322997
33 2014-10-02 10:00:00 0.3888709 0.3672786 0.7310967 0.5459476 0.4186874
34 2014-10-02 11:00:00 0.4851898 0.4332193 1.1853651 0.8819342 0.6309814
35 2014-10-02 12:00:00 0.7309699 0.6006000 1.4795510 1.2129786 0.9520352
36 2014-10-02 13:00:00 0.7772064 0.6782416 1.3742050 1.1495084 0.8788777
37 2014-10-02 14:00:00 0.5993919 0.4993290 1.2940200 1.0149044 0.7523193
38 2014-10-02 15:00:00 0.6174703 0.5152056 1.3508599 1.0936778 0.8123982
39 2014-10-02 16:00:00 0.4940848 0.4520405 1.0148175 0.8094627 0.6210421
40 2014-10-02 17:00:00 0.2838293 0.2638490 0.6576513 0.4685425 0.3297028
41 2014-10-02 18:00:00 0.2821744 0.2868666 0.5470848 0.3839681 0.2953094
42 2014-10-02 19:00:00 0.4915167 0.4486379 0.7979726 0.6808952 0.5755669
43 2014-10-02 20:00:00 0.7975491 0.7974901 0.9405753 0.8809404 0.8262123
44 2014-10-02 21:00:00 0.9996345 1.0169371 0.9848169 0.9743132 0.9496084
45 2014-10-02 22:00:00 1.1192230 1.1608159 1.0137927 1.0472195 1.0813281
46 2014-10-02 23:00:00 1.0083826 0.9924407 1.0282077 1.0308255 1.0414692
47 2014-10-03 01:00:00 0.8110946 0.7285674 0.9276852 0.9039803 0.8466192
48 2014-10-03 02:00:00 0.7049764 0.6674100 0.8118698 0.7712719 0.7342418
49 2014-10-03 03:00:00 0.5228055 0.5161498 0.7964159 0.6631099 0.5409952
50 2014-10-03 04:00:00 0.4307006 0.3925236 0.7924875 0.6516449 0.4856323
51 2014-10-03 05:00:00 0.3630006 0.3219436 0.7438930 0.5910629 0.4391163
52 2014-10-03 06:00:00 0.3227051 0.3036027 0.4723627 0.4075851 0.3498484
53 2014-10-03 07:00:00 0.2489822 0.2462210 0.3851133 0.3003173 0.2612268
54 2014-10-03 08:00:00 0.2543541 0.2528302 0.4177729 0.3199024 0.2617126
55 2014-10-03 09:00:00 0.2541252 0.2474244 0.3939348 0.3070621 0.2624668
56 2014-10-03 10:00:00 0.2569437 0.2495116 0.4621399 0.3388748 0.2649696
57 2014-10-03 11:00:00 0.2588536 0.2467579 0.5514519 0.4071498 0.2927757
58 2014-10-03 12:00:00 0.3188457 0.2771559 0.8048247 0.5899362 0.3943979
59 2014-10-03 13:00:00 0.6149420 0.4581711 1.2723467 1.1093701 0.8433346
60 2014-10-03 14:00:00 0.7987463 0.7328387 1.1159281 1.0049904 0.8697229


I want to calculate the mean of all columns over 23-hrs starting at 11AM on any one day and 10AM on the next day. So in the example above, it will ignore row number 1 to 10, then calculate average for rows 11 to 33, then 34 to 56 and so on....some time stamps may be missing so I cannot use &#39; mean of every 23 rows&#39; - it has to take the specific times into account. I checked this [answer][1], but it will not apply to my case since the averaging has to span consecutive dates, not just times. I also checked this [answer][2], but again it does not apply here since times and dates both vary. 
Any help would be much appreciated. 
Here are 200 rows for a reproducible data. 

jnk2 <- structure(list(V1 = structure(c(1412125200, 1412128800, 1412132400,
1412136000, 1412139600, 1412143200, 1412146800, 1412150400, 1412154000,
1412157600, 1412161200, 1412164800, 1412168400, 1412172000, 1412175600,
1412179200, 1412182800, 1412186400, 1412190000, 1412193600, 1412197200,
1412200800, 1412204400, 1412211600, 1412215200, 1412218800, 1412222400,
1412226000, 1412229600, 1412233200, 1412236800, 1412240400, 1412244000,
1412247600, 1412251200, 1412254800, 1412258400, 1412262000, 1412265600,
1412269200, 1412272800, 1412276400, 1412280000, 1412283600, 1412287200,
1412290800, 1412298000, 1412301600, 1412305200, 1412308800, 1412312400,
1412316000, 1412319600, 1412323200, 1412326800, 1412330400, 1412334000,
1412337600, 1412341200, 1412344800, 1412348400, 1412352000, 1412355600,
1412359200, 1412362800, 1412366400, 1412370000, 1412373600, 1412377200,
1412384400, 1412388000, 1412391600, 1412395200, 1412398800, 1412402400,
1412406000, 1412409600, 1412413200, 1412416800, 1412420400, 1412434800,
1412438400, 1412442000, 1412445600, 1412449200, 1412452800, 1412456400,
1412460000, 1412463600, 1412470800, 1412474400, 1412478000, 1412481600,
1412485200, 1412488800, 1412492400, 1412496000, 1412499600, 1412503200,
1412506800, 1412510400, 1412514000, 1412517600, 1412521200, 1412524800,
1412528400, 1412532000, 1412535600, 1412539200, 1412542800, 1412546400,
1412550000, 1412557200, 1412560800, 1412564400, 1412568000, 1412571600,
1412575200, 1412578800, 1412582400, 1412600400, 1412604000, 1412618400,
1412622000, 1412625600, 1412629200, 1412632800, 1412636400, 1412643600,
1412647200, 1412650800, 1412654400, 1412661600, 1412665200, 1412668800,
1412672400, 1412676000, 1412679600, 1412683200, 1412686800, 1412690400,
1412694000, 1412697600, 1412701200, 1412704800, 1412708400, 1412712000,
1412715600, 1412719200, 1412722800, 1412730000, 1412733600, 1412737200,
1412740800, 1412744400, 1412748000, 1412751600, 1412755200, 1412758800,
1412762400, 1412766000, 1412769600, 1412773200, 1412776800, 1412780400,
1412784000, 1412787600, 1412791200, 1412794800, 1412798400, 1412802000,
1412805600, 1412809200, 1412816400, 1412820000, 1412823600, 1412827200,
1412830800, 1412834400, 1412838000, 1412841600, 1412845200, 1412848800,
1412852400, 1412856000, 1412859600, 1412863200, 1412866800, 1412870400,
1412874000, 1412877600, 1412881200, 1412884800, 1412888400, 1412892000,
1412895600, 1412902800, 1412906400, 1412910000, 1412913600), class = c("POSIXct",
"POSIXt"), tzone = "BST"), ZDR_1100 = c(0.306166959624468, 0.304792980057827,
0.367377290916009, 0.791709029047397, 0.512739984113198, 0.488669258328616,
0.427918559009522, 0.4148057710952, 0.448494080146826, 0.358334683175604,
0.628830424426156, 0.840667803477432, 0.57762511753697, 0.423958465097973,
0.504154549025782, 0.412436032025778, 0.288864902216891, 0.257677962302261,
0.62209015211343, 1.27619467701375, 1.13741780581047, 1.01133064663052,
1.01690862635555, 0.905104896188305, 0.747320851445752, 0.99860122008246,
1.16699450222289, 1.12710798347061, 0.732141517789624, 0.411510947523374,
0.285014178127158, 0.31757623914263, 0.388870892676081, 0.485189820949329,
0.730969921207159, 0.777206352190593, 0.599391895381984, 0.617470291415275,
0.494084813832369, 0.283829303950619, 0.282174439544077, 0.49151672623415,
0.797549149155139, 0.999634500348973, 1.11922301145235, 1.00838256337839,
0.811094633383586, 0.704976379469394, 0.522805528301706, 0.430700591538227,
0.363000561294445, 0.322705100115051, 0.248982152086936, 0.254354138733661,
0.254125211972804, 0.256943729385884, 0.258853632619735, 0.318845735256057,
0.614942045199305, 0.798746327445168, 0.76665591095572, 0.590341366047632,
0.341787106321451, 0.267435215519493, 0.275926722128906, 0.313589193255248,
0.289170986426621, 0.295545324394919, 0.319455990584964, 0.322738595939772,
0.323959199488907, 0.330522877419651, 0.267571755699977, 0.29998428995512,
0.30782207876571, 0.328798936677955, 0.252428341635039, 0.36527506864465,
0.435455020375343, 0.945258900785373, 0.28536906131875, 0.392770445335228,
0.303140049236169, 0.271632188379247, 0.65099029860232, 0.673431443179817,
0.620956529062278, 0.67792393612339, 0.613146306659847, 0.560286926495226,
0.610028745815181, 0.702795085729159, 0.454744406314069, 0.564010216896549,
0.670388178492098, 0.468198619248448, 0.295333250126878, 0.377206196549991,
0.378989875356477, 0.379519630298216, 0.519521260503453, 0.489175157430295,
0.390046342489802, 0.304607913433416, 0.324769173863739, 0.258325571144312,
0.246597557429634, 0.326649457465329, 0.39291072266986, 0.416630177503529,
0.394911665991455, 0.298180506602153, 0.275368063766755, 0.265419875729781,
0.25481802718742, 0.257929650872114, 0.259432258667268, 0.550773515948454,
0.783567297829752, 0.582085689563959, 0.65548805765926, 0.417048131519081,
0.892056078453132, 0.430395276737828, 0.280147392079754, 0.256982956244017,
0.259863996920036, 0.252961891260658, 0.342380577991684, 0.350486782629869,
0.26656575505439, 0.468525904993311, 0.742653761528019, 0.245740019310022,
0.247868926676842, 0.257865002631826, 0.268768988249014, 0.356707716560482,
0.463153516453714, 0.458664708848158, 0.365650487035952, 0.335407593774495,
0.453336217532167, 0.234351097389882, 0.24521788117489, 0.278011456984742,
0.328089347084184, 0.283441332054009, 0.263668058086683, 0.253781630965119,
0.244267635323129, 0.244125356003919, 0.282797589273301, 0.641367311405213,
0.313056859455155, 0.817134083016476, 0.648253456987256, 0.417214720056331,
0.460400573207814, 0.51532566476891, 0.536269445644359, 0.358129810482984,
0.486823670767837, 0.470990868119649, 0.50119399312574, 0.544492556192248,
0.404999503369578, 0.585965088638632, 0.245547631842607, 0.245950103104953,
0.247394716929564, 0.247918267880546, 0.24514777527191, 0.244997338755948,
0.24174556619298, 0.242228135178611, 0.236388564721062, 0.242138558696654,
0.249149513254292, 0.244495321476197, 0.248338346608927, 0.26474288284952,
0.387825610559892, 0.523591783821677, 0.568263436617034, 0.30759879377778,
0.50536983300805, 0.393001240824807, 0.262616927077611, 0.484723520163478,
0.417798315528547, 0.550690797455405, 0.284779284727357, 0.272780250871231,
0.270404395113744, 0.286213321615711, 0.267682629670364, 0.252276241072987,
0.265963221673129, 0.270202912781879), ZDR_1300 = c(0.291501068071225,
0.290882921893481, 0.346500847500785, 0.747439807903032, 0.529210893066908,
0.467004588054299, 0.406475787284194, 0.405749481387455, 0.336470762434006,
0.272672452292289, 0.434885389605608, 0.588976944612851, 0.576909924269359,
0.404832762810228, 0.44621591383041, 0.331641478202067, 0.267869563447463,
0.246647086110144, 0.604517944856812, 1.33549899977436, 1.01252897967501,
0.800890749796727, 0.837841182271911, 0.805390150467188, 0.5441371681841,
0.776658811981838, 1.04753782486961, 0.886592465400665, 0.688992035465006,
0.345189449271006, 0.284264754693977, 0.308753881677813, 0.367278550226977,
0.433219252921245, 0.600600030368342, 0.678241647222608, 0.499328957666392,
0.515205586963039, 0.452040452871145, 0.263849032970031, 0.286866556101161,
0.448637875724169, 0.797490119020151, 1.01693713985887, 1.16081590389615,
0.992440670130741, 0.728567357189544, 0.667410004954317, 0.516149829889282,
0.392523622202089, 0.321943581220945, 0.303602715809219, 0.246220959882371,
0.252830231505082, 0.247424440617277, 0.249511617805284, 0.246757891692313,
0.277155882097432, 0.458171056695577, 0.732838700368049, 0.659749193254532,
0.605705656609735, 0.380481132303311, 0.269248004952531, 0.26411364637976,
0.289990656476871, 0.265754770077683, 0.27905806681813, 0.29501014768937,
0.276489971661986, 0.26443140846833, 0.270598604099728, 0.246417717395125,
0.294124956046204, 0.258454330683882, 0.267584647839097, 0.251760262361414,
0.353095340466517, 0.503845991307242, 0.986152519526138, 0.283950762989442,
0.299945949300914, 0.267892660250703, 0.247512161453397, 0.438507658086068,
0.449479966898741, 0.460429832879535, 0.517172353054133, 0.497472383658986,
0.415875692926642, 0.451803547110126, 0.447289233595495, 0.421892018797967,
0.588031115529341, 0.750745593655096, 0.472386709655424, 0.288847419221246,
0.377279452341556, 0.402555341193017, 0.361666751321716, 0.427366298743571,
0.382192190600126, 0.307580779450804, 0.274605511463274, 0.336434740699188,
0.257427066588128, 0.253430850140324, 0.293535216229256, 0.369579449061208,
0.381023246315463, 0.336804778486546, 0.266668088700018, 0.252583624833037,
0.254291453431631, 0.244976002191516, 0.253527545208668, 0.258235841872099,
0.719676790946624, 0.866057337986613, 0.727834698229293, 0.633983680165881,
0.448857786459309, 0.909133059148513, 0.434329149355426, 0.262790833152969,
0.254169275707767, 0.248083634246264, 0.247473002504987, 0.358671241595451,
0.340339249953587, 0.250609028809703, 0.467436184094897, 0.730760291783329,
0.23943371490152, 0.244575064919445, 0.248933585310931, 0.24631282634353,
0.27952839061366, 0.482141981467825, 0.458933956060562, 0.343306161669687,
0.313131312880246, 0.459396620288147, 0.236598531758105, 0.242513371944089,
0.258130763227569, 0.312922066087789, 0.268658013148464, 0.25024248251174,
0.244976754800211, 0.241482824339874, 0.236575980321351, 0.301716597539039,
0.697754807084732, 0.328946270954568, 0.856366651497727, 0.690482570343647,
0.418295455619265, 0.494909502887263, 0.422413343903637, 0.510077839382021,
0.297279599756938, 0.468153254096648, 0.419080417633641, 0.429252568759929,
0.471417534212339, 0.390985244697903, 0.616582958824039, 0.24278729114239,
0.241051681208852, 0.244897972472117, 0.243717504324991, 0.245363125257355,
0.242017539253054, 0.240890502674146, 0.237103477214922, 0.235805971226268,
0.235174887659806, 0.24272204581784, 0.238177709384284, 0.244513454055599,
0.248481076226985, 0.370052471395284, 0.488452801858902, 0.561409534974144,
0.261377438349095, 0.499799803960102, 0.378105285619023, 0.249149928761133,
0.486699243265091, 0.404083466438714, 0.577970504431472, 0.270719404698161,
0.267768494783564, 0.259197617583085, 0.262567002791191, 0.254761310553133,
0.246181575422189, 0.250614974980082, 0.252845991995895), ZDR_500 = c(0.447157848283981,
0.439697725073657, 0.424864100959259, 0.784473212558454, 0.563516778892346,
0.594755447717151, 0.627857138868487, 0.414529546241792, 0.606520586294527,
0.839763326584241, 0.907094025341837, 1.08730813227867, 0.885042578285454,
0.763741719767745, 0.745166063174876, 0.58141298185515, 0.481930807343329,
0.476950684451955, 0.670087107617181, 1.04881322387655, 1.05667877470902,
1.02797991121235, 0.995300088478629, 1.11317510052901, 1.1298674694261,
1.14127114894685, 1.2551267196612, 1.21657703527219, 0.780323955325501,
0.687076468279696, 0.530966171493146, 0.525550346866463, 0.731096706767106,
1.18536508875013, 1.47955095879362, 1.37420501517262, 1.29401996227385,
1.3508598699565, 1.01481748180098, 0.657651255526478, 0.547084756758934,
0.797972550211751, 0.940575253886622, 0.98481690294973, 1.01379265910283,
1.0282076834126, 0.927685232537129, 0.811869780626719, 0.796415887823373,
0.792487472366316, 0.743892963366656, 0.472362707503749, 0.385113263672509,
0.417772887078394, 0.393934793423819, 0.46213990527415, 0.551451944673001,
0.804824698988301, 1.27234673980559, 1.1159281194106, 0.919099112625489,
0.698366005669081, 0.492618082615144, 0.433780694737202, 0.485039908509813,
0.480790624949223, 0.472149232046432, 0.514626745025588, 0.567853397489523,
0.674741804745612, 0.721716952907861, 0.683900725231969, 0.553889453024884,
0.544309174563141, 0.602767580817208, 0.640962648972885, 0.496015767786586,
0.542857141995409, 0.445270977777479, 0.757402337346364, 0.50327027846894,
0.913866437192218, 0.656186548128403, 0.537394544303857, 0.874099321929151,
0.974608994746166, 1.03675516891411, 0.962215457598161, 0.864818647702445,
0.752479901237241, 0.882380887510225, 1.20903722660616, 0.877904192149715,
0.76411803471664, 0.636423092804689, 0.561370012186923, 0.443457825519966,
0.462576742898307, 0.469685189758837, 0.74562936393213, 1.01703457919925,
0.968974779046794, 0.714456924338609, 0.523038739243416, 0.461206099724868,
0.396251921769585, 0.389354677406272, 0.58067437933993, 0.634554773908062,
0.658892841388536, 0.621902977859412, 0.602931440954878, 0.541321727429876,
0.463202266992709, 0.401338535031121, 0.42777753683079, 0.437051950632575,
0.534851703257647, 0.816674867736689, 0.711391675879744, 0.791810703578327,
0.53704216424654, 1.00119841303614, 0.570664369934614, 0.45920963481814,
0.429078869040866, 0.402747462859592, 0.416307157767188, 0.551873204749499,
0.508302486079688, 0.475400362782827, 0.610316762442828, 0.74017089432177,
0.354194750320289, 0.409278888815564, 0.473503923554132, 0.498766985792535,
0.614053468345543, 0.524852463949865, 0.612585391980236, 0.52123870438189,
0.452744974538376, 0.57349081481997, 0.393159565658477, 0.367868471917078,
0.441289814330483, 0.436243665483414, 0.408500482764329, 0.399422247126966,
0.378041139148177, 0.375150469640102, 0.385116413151919, 0.395598681243422,
0.676348998189545, 0.474021460795992, 0.840761669609026, 0.739661636012385,
0.514155999005662, 0.475253996796394, 0.654812174928379, 0.636073767320955,
0.681810470730821, 0.793353818036636, 0.775041197104509, 0.620202692843005,
0.615223830609519, 0.481088461956559, 0.767798283524564, 0.476495413078229,
0.432534179243451, 0.39853104370495, 0.367502500097217, 0.369449840989991,
0.379184849694515, 0.361906527738415, 0.368910241794001, 0.359546731808712,
0.369193544741263, 0.390288018911529, 0.392930328275987, 0.401972309207678,
0.417441650578851, 0.52042106604402, 0.685157106333222, 0.738593160412749,
0.589218137628471, 0.616006270500352, 0.615220571764673, 0.413601751997909,
0.579144771684094, 0.562115623521554, 0.623572841837412, 0.407365413250348,
0.413323825607136, 0.415114965236609, 0.453566646285508, 0.420902944124637,
0.419227971143084, 0.445624222177617, 0.446075417266147), ZDR_700 = c(0.370788163559342,
0.370226149461672, 0.37381075226368, 0.787530007288696, 0.50368801315032,
0.545032950494832, 0.521451553856108, 0.335773048554223, 0.575355072877595,
0.717900395952568, 0.860426283184348, 1.0566411278534, 0.757667984690787,
0.673783107686814, 0.660018706625373, 0.526373976265693, 0.384118025239278,
0.353685865375689, 0.597939688761731, 1.17413202880966, 1.19570638275593,
1.10661790012339, 1.06557151404832, 1.12297729197634, 1.15111151179069,
1.22298655858139, 1.34025378688073, 1.32254032922633, 0.764169694042987,
0.607024179736095, 0.394913810381255, 0.400503509295238, 0.545947555690129,
0.881934241549427, 1.21297861207403, 1.14950836711973, 1.01490443291129,
1.09367781149235, 0.809462681831464, 0.468542452662498, 0.383968093166945,
0.68089516208681, 0.880940405712065, 0.97431322934987, 1.04721945120111,
1.03082554964447, 0.90398032534975, 0.771271940073761, 0.663109871788617,
0.651644881713707, 0.591062884986186, 0.407585054277262, 0.30031731044673,
0.319902445872562, 0.307062051419423, 0.338874762232972, 0.407149824977729,
0.589936151202355, 1.10937010015892, 1.00499043165653, 0.804282298813182,
0.553879690921826, 0.375988929187117, 0.324322731668686, 0.362721588020778,
0.399844732482978, 0.384545931770595, 0.398915991008712, 0.467436561206442,
0.578287707327679, 0.622766586535958, 0.598375134141297, 0.421640956402466,
0.434203520184606, 0.520507635633737, 0.54576427387456, 0.356702316196214,
0.447703169303208, 0.38957350859409, 0.811456731185951, 0.37175896279174,
0.792620883883221, 0.532358095337311, 0.413186126941117, 0.885457945806341,
1.02545591292483, 1.03494400787049, 0.967107419045587, 0.809892285486709,
0.732899095363973, 0.881513564391729, 1.22986369639497, 0.705169146101461,
0.674274530301116, 0.591476413153936, 0.487893938616851, 0.347140216908559,
0.384318785781984, 0.377387471352716, 0.585071189259423, 0.891766415876556,
0.808368221323358, 0.595095662255979, 0.403665852611998, 0.357510819775533,
0.313906605428132, 0.293595440562824, 0.469450783120102, 0.524960726737879,
0.562782076491615, 0.550389252765338, 0.476885807393202, 0.408497795184272,
0.359424803767168, 0.316746833389196, 0.324477527487339, 0.334266665104247,
0.448536879746686, 0.756400916489099, 0.598719578563839, 0.762305729286618,
0.441816217990686, 0.985582108022832, 0.508211541072629, 0.371205767576918,
0.331656380186518, 0.318025866154805, 0.323021877288779, 0.424361870947883,
0.431585032471921, 0.36545281524856, 0.511509835243599, 0.737667170324339,
0.286839004797462, 0.309689076873744, 0.359740126332276, 0.398308116719114,
0.558704123906597, 0.468003714078628, 0.512873342252673, 0.435191682951981,
0.377976851692703, 0.492331848640619, 0.299802041791769, 0.293414631236892,
0.351440864712271, 0.3683106627361, 0.337144932918378, 0.318796702007742,
0.299289928949139, 0.288246971932262, 0.301061359457024, 0.310007931996722,
0.649741539077279, 0.386690053141616, 0.82872244262503, 0.6967591966577,
0.455444825675274, 0.43204695542734, 0.64983916863904, 0.602022201419539,
0.572208455630965, 0.686751691461162, 0.670038212808726, 0.579702761846133,
0.603152291386814, 0.432255704258182, 0.662113904323771, 0.341915409978099,
0.330201567366411, 0.304361919516635, 0.287141651971348, 0.287330760746907,
0.297920272667372, 0.284874070669093, 0.291747974122913, 0.281505942801872,
0.294843489699713, 0.302899161224661, 0.299395925841937, 0.307296474647003,
0.338551637347388, 0.444212273938106, 0.623486180565142, 0.649318675779244,
0.487335177668373, 0.538892780190712, 0.537253326010574, 0.340758629667048,
0.508731303094864, 0.479953499107758, 0.56648697416758, 0.33409131998203,
0.332939592743565, 0.329822484003441, 0.365017001565423, 0.335953252019529,
0.325987717382123, 0.353377167038282, 0.355124095734524), ZDR_900 = c(0.322013094612601,
0.326401619359454, 0.36120575570146, 0.790194803107237, 0.498746564227196,
0.500273526466069, 0.45998713801043, 0.357591428291511, 0.544415728426255,
0.514855101944107, 0.780747675395302, 0.984153677379189, 0.630357564900658,
0.531364386192344, 0.569540443809864, 0.475226233484721, 0.315239166135564,
0.282144057883886, 0.606121260307091, 1.25812241373755, 1.27879910391279,
1.10514384291408, 1.133691237433, 1.04723623821353, 0.99126852799521,
1.13569942747937, 1.30453924461453, 1.30538868371152, 0.74988861765306,
0.501528437107879, 0.314257602241638, 0.332299734369396, 0.418687382566315,
0.630981354144116, 0.9520352055469, 0.878877701357596, 0.752319343760261,
0.812398172871216, 0.621042100387632, 0.329702750014081, 0.295309441220396,
0.575566861789565, 0.826212337165651, 0.949608416717571, 1.08132809597801,
1.04146918730583, 0.846619170566966, 0.734241775583096, 0.540995158004246,
0.485632303780237, 0.439116327474204, 0.349848414270195, 0.261226849107972,
0.261712645244219, 0.262466839685866, 0.264969637952237, 0.292775689036612,
0.394397867786279, 0.843334642409884, 0.869722858034248, 0.746004041024809,
0.510316757322993, 0.310387207477211, 0.267834088674584, 0.293469866921824,
0.338621085374035, 0.312682227629955, 0.325302527808893, 0.370110290664656,
0.424524286694403, 0.460234071847573, 0.465294504583448, 0.306550561776607,
0.337686441685322, 0.396143586370905, 0.431481541083807, 0.268510065054271,
0.39241712502709, 0.369377463226119, 0.856125359500893, 0.298637251849439,
0.593153329465965, 0.389587237970951, 0.309645463686251, 0.831307948408553,
0.926839012260825, 0.87153670405866, 0.848274995468669, 0.723067805892763,
0.680202083896971, 0.775922494670726, 1.00865484538561, 0.546347339684526,
0.565009302332728, 0.593754671344316, 0.444954604474001, 0.298680509253058,
0.360070268061248, 0.352747220895493, 0.428940224690434, 0.691963836479632,
0.632358476875149, 0.482357330152312, 0.331370003657648, 0.313104739502789,
0.260080779401281, 0.246603005155057, 0.375555199708371, 0.428057581625897,
0.475410237234394, 0.455960683834185, 0.360709804544475, 0.316576061525939,
0.293290594670002, 0.269085306654159, 0.266433714210227, 0.27165455812222,
0.455796736833075, 0.728582560445545, 0.539499024101169, 0.659749886199089,
0.366600649514378, 0.965941845562007, 0.463789889836903, 0.313255071168293,
0.278043152026169, 0.264778607098185, 0.269064585052694, 0.35483117241126,
0.369927837030562, 0.293050427902309, 0.465433060587079, 0.736598207813794,
0.256680193236605, 0.253235419626944, 0.273008962504733, 0.318284489519279,
0.471479353799458, 0.454040454930391, 0.464952295126241, 0.384258981363303,
0.337978074853723, 0.449757088772328, 0.247299535953423, 0.255091902688139,
0.29861803864712, 0.335130867676161, 0.291261562360203, 0.270414126081328,
0.256914953944704, 0.250339643201024, 0.254306669836962, 0.273634333945517,
0.63908287009702, 0.323931923588005, 0.840099752577933, 0.652665451878606,
0.423298255792864, 0.436412548343414, 0.636986263779029, 0.558324265458623,
0.454406574992519, 0.564588397259814, 0.552650158614149, 0.570337397119438,
0.588406306823723, 0.409689172095912, 0.601875122531951, 0.265603001377748,
0.262930491238631, 0.255929860554701, 0.252764844754657, 0.244484812716529,
0.252724288095977, 0.244213654562723, 0.251582247899416, 0.24187326965045,
0.255021905187916, 0.258061831487029, 0.252383073540193, 0.258888998788625,
0.285172913944117, 0.417567673070375, 0.576011825201839, 0.585338397737689,
0.384593212027856, 0.506835248360119, 0.463156381824222, 0.292494675578966,
0.479322850034583, 0.43172587853861, 0.535396488556011, 0.291657208127747,
0.286002316545171, 0.279663747591679, 0.309409468098404, 0.282038371654495,
0.271265488998737, 0.293524702814129, 0.294515128781904)), row.names = c(NA,
200L), class = "data.frame")


[1]: https://stackoverflow.com/questions/47952129/how-can-i-find-the-mean-between-two-time-rangeposixct-in-r
[2]: https://stackoverflow.com/questions/67521209/averaging-data-at-specific-hours-across-days-in-r
</details>
# 答案1
**得分**: 1
你可以使用 `time_summarise()` 函数,它将你的日期时间数据分组到24小时的时间段(右开区间),然后以这些时间段作为分组进行均值汇总。
```{r}
# remotes::install_github(&quot;NicChr/timeplyr&quot;)
library(tidyverse)
library(timeplyr)
jnk2 &lt;- as_tibble(jnk2)
start &lt;- min(jnk2$V1[hour(jnk2$V1) &gt;= 11])
jnk2 %&gt;%
time_summarise(time = V1, time_by = days(1), across(starts_with(&quot;ZDR&quot;),  mean),
from = start, include_interval = TRUE)
# 一个 tibble: 10 x 7
V1                  interval                                         ZDR_1100 ZDR_1300 ZDR_500 ZDR_700 ZDR_900
&lt;dttm&gt;              &lt;Interval&gt;                                          &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
1 2014-10-01 11:00:00 2014-10-01 11:00:00 UTC--2014-10-02 11:00:00 UTC    0.699    0.606   0.863   0.838   0.785
2 2014-10-02 11:00:00 2014-10-02 11:00:00 UTC--2014-10-03 11:00:00 UTC    0.559    0.525   0.864   0.734   0.617
3 2014-10-03 11:00:00 2014-10-03 11:00:00 UTC--2014-10-04 11:00:00 UTC    0.378    0.348   0.640   0.526   0.423
4 2014-10-04 11:00:00 2014-10-04 11:00:00 UTC--2014-10-05 11:00:00 UTC    0.526    0.453   0.757   0.703   0.614
5 2014-10-05 11:00:00 2014-10-05 11:00:00 UTC--2014-10-06 11:00:00 UTC    0.380    0.373   0.602   0.494   0.411
6 2014-10-06 11:00:00 2014-10-06 11:00:00 UTC--2014-10-07 11:00:00 UTC    0.390    0.386   0.543   0.463   0.406
7 2014-10-07 11:00:00 2014-10-07 11:00:00 UTC--2014-10-08 11:00:00 UTC    0.387    0.384   0.507   0.440   0.402
8 2014-10-08 11:00:00 2014-10-08 11:00:00 UTC--2014-10-09 11:00:00 UTC    0.336    0.320   0.495   0.413   0.361
9 2014-10-09 11:00:00 2014-10-09 11:00:00 UTC--2014-10-10 04:00:00 UTC    0.364    0.351   0.520   0.439   0.386
10 NA                  NA--NA                                              0.442    0.409   0.574   0.510   0.468

时区被编码为 "BST",这是无效的,应该是 "GB",所以我暂时使用了 UTC 以保持时间匹配示例。

另一种更直接的方法,不使用 timeplyr。

jnk2 %&gt;%
mutate(start_time = min(V1[hour(V1) &gt;= 11])) %&gt;%
mutate(time_diff = as.double(difftime(V1, start_time,, units = &quot;hours&quot;)) / (24)) %&gt;%
mutate(agg = if_else(V1 &lt; start_time, NA, start_time + (trunc(time_diff) * (24 * 60 * 60)))) %&gt;%
mutate(rowid = row_number()) %&gt;%
group_by(agg) %&gt;%
summarise(across(starts_with(&quot;ZDR&quot;),  mean), 
start_row = min(rowid),
end_row = max(rowid))
# 一个 tibble: 10 x 8
agg                 ZDR_1100 ZDR_1300 ZDR_500 ZDR_700 ZDR_900 start_row end_row
&lt;dttm&gt;                 &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;     &lt;int&gt;   &lt;int&gt;
1 2014-10-01 11:00:00    0.699    0.606   0.863   0.838   0.785        11      33
2 2014-10-02 11:00:00    0.559    0.525   0.864   0.734   0.617        34      56
3 2014-10-03 11:00:00    0.378    0.348   0.640   0.526   0.423        57      79
4 2014-10-04 11:00:00    0.526    0.453   0.757   0.703   0.614        80      99
5 2014-10-05 11:00:00    0.380    0.373   0.602   0.494   0.411       100     120
6 2014-10-06 11:00:00    0.390    0.386   0.543   0.463   0.406       121     137
7
<details>
<summary>英文:</summary>
You can use `time_summarise()` which groups your datetime into 24-hour bins (right-interval open) and uses these as groups for the mean summary.
```{r}
# remotes::install_github(&quot;NicChr/timeplyr&quot;)
library(tidyverse)
library(timeplyr)
jnk2 &lt;- as_tibble(jnk2)
start &lt;- min(jnk2$V1[hour(jnk2$V1) &gt;= 11])
jnk2 %&gt;%
time_summarise(time = V1, time_by = days(1), across(starts_with(&quot;ZDR&quot;),  mean),
from = start, include_interval = TRUE)
# A tibble: 10 x 7
V1                  interval                                         ZDR_1100 ZDR_1300 ZDR_500 ZDR_700 ZDR_900
&lt;dttm&gt;              &lt;Interval&gt;                                          &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
1 2014-10-01 11:00:00 2014-10-01 11:00:00 UTC--2014-10-02 11:00:00 UTC    0.699    0.606   0.863   0.838   0.785
2 2014-10-02 11:00:00 2014-10-02 11:00:00 UTC--2014-10-03 11:00:00 UTC    0.559    0.525   0.864   0.734   0.617
3 2014-10-03 11:00:00 2014-10-03 11:00:00 UTC--2014-10-04 11:00:00 UTC    0.378    0.348   0.640   0.526   0.423
4 2014-10-04 11:00:00 2014-10-04 11:00:00 UTC--2014-10-05 11:00:00 UTC    0.526    0.453   0.757   0.703   0.614
5 2014-10-05 11:00:00 2014-10-05 11:00:00 UTC--2014-10-06 11:00:00 UTC    0.380    0.373   0.602   0.494   0.411
6 2014-10-06 11:00:00 2014-10-06 11:00:00 UTC--2014-10-07 11:00:00 UTC    0.390    0.386   0.543   0.463   0.406
7 2014-10-07 11:00:00 2014-10-07 11:00:00 UTC--2014-10-08 11:00:00 UTC    0.387    0.384   0.507   0.440   0.402
8 2014-10-08 11:00:00 2014-10-08 11:00:00 UTC--2014-10-09 11:00:00 UTC    0.336    0.320   0.495   0.413   0.361
9 2014-10-09 11:00:00 2014-10-09 11:00:00 UTC--2014-10-10 04:00:00 UTC    0.364    0.351   0.520   0.439   0.386
10 NA                  NA--NA                                              0.442    0.409   0.574   0.510   0.468

The timezone was coded as "BST" which isn't valid, and should be "GB" so I've just used UTC for the time being so the clock times match with the example.

An alternative more direct approach without timeplyr.

jnk2 %&gt;%
mutate(start_time = min(V1[hour(V1) &gt;= 11])) %&gt;%
mutate(time_diff = as.double(difftime(V1, start_time,, units = &quot;hours&quot;)) / (24)) %&gt;%
mutate(agg = if_else(V1 &lt; start_time, NA, start_time + (trunc(time_diff) * (24 * 60 * 60)))) %&gt;%
mutate(rowid = row_number()) %&gt;%
group_by(agg) %&gt;%
summarise(across(starts_with(&quot;ZDR&quot;),  mean), 
start_row = min(rowid),
end_row = max(rowid))
# A tibble: 10 x 8
agg                 ZDR_1100 ZDR_1300 ZDR_500 ZDR_700 ZDR_900 start_row end_row
&lt;dttm&gt;                 &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;     &lt;int&gt;   &lt;int&gt;
1 2014-10-01 11:00:00    0.699    0.606   0.863   0.838   0.785        11      33
2 2014-10-02 11:00:00    0.559    0.525   0.864   0.734   0.617        34      56
3 2014-10-03 11:00:00    0.378    0.348   0.640   0.526   0.423        57      79
4 2014-10-04 11:00:00    0.526    0.453   0.757   0.703   0.614        80      99
5 2014-10-05 11:00:00    0.380    0.373   0.602   0.494   0.411       100     120
6 2014-10-06 11:00:00    0.390    0.386   0.543   0.463   0.406       121     137
7 2014-10-07 11:00:00    0.387    0.384   0.507   0.440   0.402       138     160
8 2014-10-08 11:00:00    0.336    0.320   0.495   0.413   0.361       161     183
9 2014-10-09 11:00:00    0.364    0.351   0.520   0.439   0.386       184     200
10 NA                     0.442    0.409   0.574   0.510   0.468         1      10

答案2

得分: 0

jnk2 %&gt;%
mutate(day = as_date(ifelse(hms(format(V1, &quot;%H:%M:%S&quot;)) &gt; (11*60*60), as_date(V1), as_date(V1) - 1))) %&gt;%
summarise(across(starts_with(&quot;ZDR&quot;), mean), .by= day)
# 一个数据框: 10 &#215; 6
day        ZDR_1100 ZDR_1300 ZDR_500 ZDR_700 ZDR_900
&lt;date&gt;        &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
1 2014-09-30    0.459    0.412   0.605   0.542   0.496
2 2014-10-01    0.693    0.606   0.875   0.839   0.778
3 2014-10-02    0.549    0.517   0.836   0.713   0.602
4 2014-10-03    0.407    0.380   0.649   0.544   0.447
5 2014-10-04    0.498    0.422   0.756   0.691   0.592
6 2014-10-05    0.380    0.373   0.595   0.490   0.410
7 2014-10-06    0.388    0.380   0.547   0.468   0.410
8 2014-10-07    0.395    0.394   0.508   0.442   0.406
9 2014-10-08    0.335    0.319   0.497   0.414   0.362
10 2014-10-09    0.354    0.343   0.510   0.428   0.374
英文:
jnk2 %&gt;%
mutate(day = as_date(ifelse(hms(format(V1, &quot;%H:%M:%S&quot;)) &gt; (11*60*60), as_date(V1), as_date(V1) - 1))) %&gt;%
summarise(across(starts_with(&quot;ZDR&quot;), mean), .by= day)
# A tibble: 10 &#215; 6
day        ZDR_1100 ZDR_1300 ZDR_500 ZDR_700 ZDR_900
&lt;date&gt;        &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;
1 2014-09-30    0.459    0.412   0.605   0.542   0.496
2 2014-10-01    0.693    0.606   0.875   0.839   0.778
3 2014-10-02    0.549    0.517   0.836   0.713   0.602
4 2014-10-03    0.407    0.380   0.649   0.544   0.447
5 2014-10-04    0.498    0.422   0.756   0.691   0.592
6 2014-10-05    0.380    0.373   0.595   0.490   0.410
7 2014-10-06    0.388    0.380   0.547   0.468   0.410
8 2014-10-07    0.395    0.394   0.508   0.442   0.406
9 2014-10-08    0.335    0.319   0.497   0.414   0.362
10 2014-10-09    0.354    0.343   0.510   0.428   0.374

Where 'day' is the date of the start of each period.

huangapple
  • 本文由 发表于 2023年7月24日 19:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定