{"id":557,"date":"2018-10-08T02:03:24","date_gmt":"2018-10-08T09:03:24","guid":{"rendered":"https:\/\/blogs.ubc.ca\/yiwang28\/?p=557"},"modified":"2018-10-08T02:04:19","modified_gmt":"2018-10-08T09:04:19","slug":"my-r-learning-notes-2-a-simple-way-to-calculate-the-daily-mean-from-hourly-data-with-a-different-time-interval","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/yiwang28\/2018\/10\/08\/my-r-learning-notes-2-a-simple-way-to-calculate-the-daily-mean-from-hourly-data-with-a-different-time-interval\/","title":{"rendered":"My R learning notes 2: a simple way to calculate the daily mean from hourly data with a different time interval"},"content":{"rendered":"<p>Recently I received a surprising email from a scholar that I haven&#8217;t met before. He told me that he came across my blog about aggregating minutely data into hourly data when he was trying to calculate the daily mean from a different time interval (e.g., from 09:00 am to 08:00 am at the following day).<\/p>\n<p>At first, I was so surprised, as I thought that this blog could not be indexed by Google. Then, I felt a little bit embarrassed, because I\u00a0didn&#8217;t write the posts very carefully, so the contents must have lots of grammar errors, and might not make sense. However, I felt happy in the end, as it was wonderful to know that my notes had enlightened others who faced the same problem. So I want to express my gratitude to all who have read my posts and borne my poor writing. I also hope to receive opinions from every reader \ud83d\ude42<\/p>\n<p>In this post, I would like to share my idea with you on how to calculate daily mean with a different time interval (e.g., from 9:00 am to 8:00 at the other day). I think that the quickest way is to calculate the mean of each column by every 24 rows in the data frame. For example, here is the initial data sheet:<\/p>\n<pre><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-565 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture1.jpg\" alt=\"\" width=\"903\" height=\"425\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture1.jpg 903w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture1-300x141.jpg 300w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture1-768x361.jpg 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/>\r\n                        Fig. 1 Initial data example<\/pre>\n<p>In order to make the R code simple, I delete the first two rows, so that the record started from 9:00 am. Now, let&#8217;s put the data frame into R as data1.<\/p>\n<pre><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-567\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture2.jpg\" alt=\"\" width=\"1169\" height=\"500\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture2.jpg 1169w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture2-300x128.jpg 300w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture2-768x328.jpg 768w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2018\/10\/Capture2-1024x438.jpg 1024w\" sizes=\"auto, (max-width: 1169px) 100vw, 1169px\" \/>\r\n                      Fig. 2 Data arrangement in R.<\/pre>\n<p>Then, let&#8217;s build a simple function\u00a0to calculate the mean of every 24 rows.The code was taken from Vlo&#8217;s answer from the following website: https:\/\/stackoverflow.com\/questions\/30359427\/calculate-the-mean-of-every-13-rows-in-data-frame.\u00a0 If you would like to calculate the sum instead of the mean, please just adjust the\u00a0 &#8220;FUN=\u00a0 \u00a0 \u00a0 &#8221; commend.<\/p>\n<p><span style=\"color: #333399;\">n.colmeans = function(df, n = 24){<\/span><br \/>\n<span style=\"color: #333399;\">aggregate(x = df,<\/span><br \/>\n<span style=\"color: #333399;\">by = list(gl(ceiling(nrow(df)\/n), n)[1:nrow(df)]),<\/span><br \/>\n<span style=\"color: #333399;\">FUN = mean)<\/span><br \/>\n<span style=\"color: #333399;\">} <span style=\"color: #99cc00;\">### because I have 12 column in the data frame, so n &gt; 12. This function will calculate the average of every 24 rows. For example, the result comes from row 1 to row 24, then from row 25 to row 49, etc.)<\/span><\/span><\/p>\n<p>After we run the above codes, we input the following code:<\/p>\n<p><span style=\"color: #333399;\">data2&lt;- n.colmeans(data1, 24<\/span>) <span style=\"color: #99cc00;\">### this is\u00a0used to apply the above function to calculate our own dataset.<\/span><\/p>\n<p>The\u00a0daily means of every column in the data2 were calculated with the time interval from 9:00 am to 8:00 at the other day. You can export the result by using the following code:<\/p>\n<p><span style=\"color: #333399;\">data2 &lt;-write.xlsx(\u00a0 )<\/span><\/p>\n<p>Finally we got what we want. Yay! However, it is note that If your data have missing rows, then this method is not applicable.<\/p>\n<p>There are definitely other ways to aggregate the data, but the codes in this blog, in my opinion, is the most simple ones. If you have different opinions, or you&#8217;d like to share your codes, please feel free to leave your comments.<\/p>\n<p>Thank you for reading it! \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I received a surprising email from a scholar that I haven&#8217;t met before. He told me that he came across my blog about aggregating minutely data into hourly data when he was trying to calculate the daily mean from a different time interval (e.g., from 09:00 am to 08:00 am at the following day). &hellip; <a href=\"https:\/\/blogs.ubc.ca\/yiwang28\/2018\/10\/08\/my-r-learning-notes-2-a-simple-way-to-calculate-the-daily-mean-from-hourly-data-with-a-different-time-interval\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">My R learning notes 2: a simple way to calculate the daily mean from hourly data with a different time interval<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":40393,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-blogs"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/users\/40393"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":9,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":569,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/557\/revisions\/569"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}