My R learning notes 2: a simple way to calculate the daily mean from hourly data with a different time interval

Recently I received a surprising email from a scholar that I haven’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).

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 didn’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 🙂

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:

                        Fig. 1 Initial data example

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’s put the data frame into R as data1.

                      Fig. 2 Data arrangement in R.

Then, let’s build a simple function to calculate the mean of every 24 rows.The code was taken from Vlo’s answer from the following website:  If you would like to calculate the sum instead of the mean, please just adjust the  “FUN=      ” commend.

n.colmeans = function(df, n = 24){
aggregate(x = df,
by = list(gl(ceiling(nrow(df)/n), n)[1:nrow(df)]),
FUN = mean)
} ### because I have 12 column in the data frame, so n > 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.)

After we run the above codes, we input the following code:

data2<- n.colmeans(data1, 24) ### this is used to apply the above function to calculate our own dataset.

The daily 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:

data2 <-write.xlsx(  )

Finally we got what we want. Yay! However, it is note that If your data have missing rows, then this method is not applicable.

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’d like to share your codes, please feel free to leave your comments.

Thank you for reading it! 🙂





154 thoughts on “My R learning notes 2: a simple way to calculate the daily mean from hourly data with a different time interval

  1. Hey there! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.|

    1. Hey Twanna! Thanks for reading the post!

      I am sorry that you had issues with hackers. I uses WordPress and it works fine 😉

  2. Hi there would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a reasonable price? Cheers, I appreciate it!

  3. Its such as you learn my mind! You appear to know a lot approximately this, such as
    you wrote the ebook in it or something. I feel that you could
    do with a few % to pressure the message house
    a little bit, however instead of that, this is fantastic blog.
    An excellent read. I will certainly be back.

  4. Simply wish to say your article is as surprising.

    The clarity for your publish is just great and that i could suppose you’re knowledgeable on this subject.
    Fine together with your permission let me to take hold of your feed to keep up to date with drawing close post.
    Thank you a million and please keep up the gratifying work.

  5. Hello There. I found your weblog using msn. This is a very smartly written article.
    I will be sure to bookmark it and come back to learn extra of your helpful information. Thanks for the post.
    I’ll certainly return.

  6. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a
    comment is added I get four e-mails with the same comment.
    Is there any way you can remove me from that service?


    1. Thanks for pointing this out! To be honest, I don’t know how to fix it. But anyway I hope you enjoyed my posts 🙂

  7. I am really impressed with your writing skills as well as
    with the layout on your blog. Is this a paid theme or did you
    modify it yourself? Anyway keep up the nice quality writing, it is rare to see a nice blog like this one today.

  8. You actually make it seem so easy with your presentation but I find this
    matter to be really something that I think I would never understand.
    It seems too complex and extremely broad for me.
    I’m looking forward for your next post, I will try to get the hang of

  9. An outstanding share! I’ve just forwarded this onto a coworker
    who had been conducting a little research on this.
    And he in fact ordered me lunch simply because I discovered
    it for him… lol. So let me reword this….

    Thank YOU for the meal!! But yeah, thanx for spending time to discuss this topic here on your internet site.

  10. Excellent blog here! Also your site loads up fast!
    What web host are you using? Can I get your affiliate link to
    your host? I wish my web site loaded up as quickly as yours lol

  11. It’s a pity you don’t have a donate button! I’d without a doubt donate to this brilliant blog!
    I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.

    I look forward to new updates and will share this website with my Facebook group.

    Chat soon!

  12. Hi would you mind sharing which blog platform you’re
    using? I’m looking to start my own blog soon but I’m having a
    hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something
    completely unique. P.S My apologies for being off-topic but I had to ask!

  13. I truly appreciate this post. I’ve been looking
    everywhere for this! Thank goodness I found it on Bing.

    You’ve made my day! Thanks again!

  14. At this time it sounds like Movable Type is the best blogging platform out there right now.
    (from what I’ve read) Is that what you’re using on your blog?

  15. Currently it sounds like Expression Engine iis the best blogging pltform available right
    now. (from what I’ve read) Is that what you
    are using on your blog?

  16. Excellent goods from you, man. I have have in mind your stuff
    prior to and you’re just too great. I really like what you’ve got right here, really like what you’re stating and the way
    in which wherein you assert it. You make it enjoyable and you still care
    for to keep it smart. I cant wait to learn far more from you.
    This is really a great web site.

  17. My partner and I absolutely love your blog and find a lot of your post’s to be what precisely I’m
    looking for. Would you offer guest writers to write content for you?

    I wouldn’t mind creating a post or elaborating on many of
    the subjects you write in relation to here. Again, awesome site!

  18. I think this is among the most vital information for me.

    And i am glad reading your article. But wanna remark on some general things, The site style is great,
    the articles is really excellent : D. Good job, cheers

  19. You actually make it seem so easy with your presentation but I find this topic
    to be really something that I think I would never understand.

    It seems too complex and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of

  20. Hi, constantly i used to check web site posts here in the early hours in the
    morning, for the reason that i like to learn more and more.

  21. I don’t even know the way I ended up here, but I believed
    this publish was once great. I don’t realize who you are but certainly you’re going to a well-known blogger if you happen to
    are not already. Cheers!

  22. Hi there very cool web site!! Guy .. Excellent ..
    Amazing .. I will bookmark your site and take the feeds also?
    I am happy to seek out numerous helpful info here within the publish, we’d like develop more strategies on this regard,
    thanks for sharing. . . . . .

  23. Hi i am kavin, its my first time to commenting anywhere, when i read this article
    i thought i could also create comment due to this good

  24. I do not even know the way I ended up right here, however I thought this post was good.

    I don’t recognize who you might be however definitely you are going to a well-known blogger if you happen to aren’t already.

  25. Greetings from Florida! I’m bored to death at work so I
    decided to browse your blog on my iphone during lunch break.
    I really like the info you provide here and can’t
    wait to take a look when I get home. I’m surprised at how quick your blog loaded on my phone ..

    I’m not even using WIFI, just 3G .. Anyways,
    very good site!

  26. Its like you read my mind! You appear to know so much about this,
    like you wrote the book in it or something. I think that you
    can do with a few pics to drive the message home a bit, but other
    than that, this is wonderful blog. A great read. I will certainly be

  27. Howdy! This blog post couldn’t be written much better!

    Looking at this article reminds me of my previous roommate!
    He continually kept preaching about this. I most certainly will send this post
    to him. Pretty sure he’s going to have a great read.

    Many thanks for sharing!

  28. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your website?
    My blog is in the exact same area of interest as yours and my visitors
    would genuinely benefit from a lot of the information you provide here.
    Please let me know if this alright with you. Many thanks!

  29. Thanks for any other informative web site. Where else could I get that
    kind of information written in such an ideal method?
    I have a project that I am simply now operating on,
    and I’ve been on the glance out for such information.

    1. Hi Franziska, thanks for reading my post! I like to visit stack overflow to learn statistical methods, and visit some websites of peer-reviewed journals related to my study to know the advance knowledge of the research. I will keep post my reflections time to time 🙂

  30. Definitely imagine that which you stated. Your favourite justification seemed to be at the internet the simplest thing to bear in mind of.
    I say to you, I certainly get annoyed at the same time as other folks consider worries that they just
    don’t recognise about. You managed to hit the nail upon the highest and also defined
    out the whole thing with no need side effect , folks could take a signal.
    Will probably be back to get more. Thank you

  31. With havin so much content and articles do you ever run into any issues of
    plagorism or copyright infringement? My site has
    a lot of exclusive content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over
    the web without my authorization. Do you know any solutions to help reduce content from being stolen? I’d definitely appreciate it.

    1. Hi thank you for browsing my blog! I didn’t run into this issue, but I hope that people who would like to share your posts in their website will gain your approval in advance 🙂

  32. I do believe all the concepts you have introduced for your post.

    They are really convincing and can certainly work. Nonetheless, the posts
    are very brief for beginners. May you please prolong them a bit from next time?
    Thank you for the post.

  33. Hey There. I discovered your weblog the usage of msn. That is a very neatly written article.
    I will make sure to bookmark it and come back to read extra of your helpful info.

    Thanks for the post. I will definitely comeback.

  34. Have you ever thought about writing an e-book or guest
    authoring on other sites? I have a blog centered on the same ideas you discuss and would love to have you share some
    stories/information. I know my viewers would appreciate your work.

    If you are even remotely interested, feel free to shoot me an e mail.

  35. I blog quite often and I genuinely appreciate your content.
    This great article has truly peaked my interest.
    I will bookmark your website and keep checking for new information about once a week.
    I subscribed to your RSS feed too.

  36. Having read this I thought it was really informative. I appreciate you taking
    the time and energy to put this content together. I once again find myself personally spending way too much time both reading and
    leaving comments. But so what, it was still worthwhile!

  37. Thanks a bunch for sharing this with all people you really realize what
    you are talking approximately! Bookmarked. Please also consult with my website =).
    We could have a link trade arrangement between us

  38. Very good written story. It will be beneficial to everyone who employess it, as
    well as yours truly :). Keep doing what you are doing
    – i will definitely read more posts.

  39. A person essentially assist to create severely posts
    I might state. That is the first time I frequented your website page
    and to the point? I surprised together with the research you made to
    create this actual post incredible. Excellent task!

    Also visit my webpage – PaulHStaiger

  40. Nice blog here! Also your website loads up very fast! What host are you using?
    Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  41. Right here is the right website for anybody who would like to understand this topic.

    You know so much its almost tough to argue with you
    (not that I actually would want to…HaHa). You definitely put a brand new spin on a topic that has been written about for decades.
    Excellent stuff, just wonderful!

  42. We would like to thank you yet again for the gorgeous ideas you offered
    Jesse when preparing her post-graduate research plus, most importantly, regarding providing
    the many ideas in one blog post. If we had known of your web-site a year
    ago, we might have been saved the useless measures we were
    employing. Thanks to you.

  43. It’s perfect time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I want to suggest
    you few interesting things or advice. Perhaps you could write next articles referring to this article.
    I wish to read more things about it!

  44. Thank you for another fantastic article. Where else
    could anyone get that type of information in such a perfect method of writing?
    I’ve a presentation next week, and I’m on the search for such information.

  45. I’m not sure where you are getting your information, but
    good topic. I needs to spend some time learning more or understanding more.

    Thanks for excellent info I was looking for this info for my mission.

  46. Hello there, I found your web site via Google even as searching for a comparable matter, your web site
    came up, it appears great. I have bookmarked it in my google bookmarks.

    Hi there, simply became aware of your weblog through Google,
    and found that it’s truly informative. I’m going to
    watch out for brussels. I will be grateful when you proceed this in future.
    Many other folks shall be benefited from your writing.

  47. Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam feedback?
    If so how do you stop it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any assistance is
    very much appreciated.

  48. Hey! I was able to have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s a novice to me.

    Anyhow, I’m definitely happy I came across it and I’ll be
    book-marking and checking back often!

    My webpage – ColeDSideman

  49. Hi Yi
    I am happy that you have shared the question I raised a year ago. Indeed the function you wrote helped me.

    However, I found it very challenging to aggregate rainfall as sum using the function.
    Is there a way I can select this rainfall column and aggregate is as sum of the same time period 900am 800am of the other day?

Leave a Reply

Your email address will not be published. Required fields are marked *