# 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: https://stackoverflow.com/questions/30359427/calculate-the-mean-of-every-13-rows-in-data-frame.  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. yi wang says:

Hey Twanna! Thanks for reading the post!

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

2. Hello.This post was really interesting, particularly because I was looking for thoughts on this subject last Monday.

1. yi wang says:

Hey thanks for reading it!

3. 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!

1. yi wang says:

Hey Rolande! Thanks for reading the post!

I was not sure if you asked the blog platform. I am using WordPress.

4. This is a topic that is close to my heart…
Take care! Exactly where are your contact details though?

5. Aw, this was an exceptionally good post. Spending some time and actual effort to produce a good article… but what can I say… I
hesitate a whole lot and never seem to get anything done.

6. Hey there! Would you mind if I share your blog with my twitter
group? There’s a lot of people that I think would really enjoy your content.
Please let me know. Thank you

7. I every time emailed this weblog post page to all my contacts, for the reason that if like to read it
next my contacts will too.

8. 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.

9. 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.

10. Hey There. I found your blog using msn. This is an extremely well written article.
I will make sure to bookmark it and come back to read
more of your useful information. Thanks for the post. I will certainly
comeback.

11. This website was… how do you say it? Relevant!! Finally I’ve found something which helped me.

Cheers!

1. yi wang says:

Glad to know that! Thank you for browsing my blog!

12. 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.

1. yi wang says:

Thank you Ferne!

13. Link exchange is nothing else however it is simply placing the other person’s web site
link on your page at proper place and other person will also do similar for you.

1. yi wang says:

Thank you for your suggestion!

14. You could certainly see your expertise within the work you write.

The arena hopes for more passionate writers such as you who aren’t
afraid to mention how they believe. All the time follow your heart.

1. yi wang says:

Thank you!

15. 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?

Cheers!

1. yi wang says:

I don’t know how to fix this problem. Sorry for the bothering!

16. If you wish for to obtain a good deal from this paragraph then you have to apply such methods to your won weblog.

17. Hello, Neat post. There is an issue together with your website in web explorer, may
check this? IE still is the market chief and
a big portion of people will pass over your excellent writing because of
this problem.

1. yi wang says:

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

18. 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.

1. yi wang says:

Thank you! It is a free theme and I just modified it by myself.

19. 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
it!

1. yi wang says:

Thank you!

20. 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.

1. yi wang says:

Thank you!

21. 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

1. yi wang says:

Hi Jeffery, thanks for browsing my blog! I am using WordPress 🙂

22. 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!

1. yi wang says:

Thank you!

23. I believe you have mentioned some very interesting
details, thank you for the post.

1. yi wang says:

Thank you!

24. 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!

1. yi wang says:

Hi I am using WordPress 🙂

25. It’s hard to find knowledgeable people about this subject, however, you seem
like you know what you’re talking about! Thanks

1. yi wang says:

Thank you!

26. 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!

27. I appreciate, lead to I discovered exactly hat
I was looking for. You have ended my 4 day long hunt! God Bless you man. Have a nice day.
Bye

1. yi wang says:

Thank you!

28. 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?

29. I visited many blogs however the audio quality
for audio songs current at this web page is truly wonderful.

30. I genuinely enjoy looking at on this internet site, it has good articles.

1. yi wang says:

Thank you!

31. I think you have remarked some very interesting details, thank you for the
post.

32. 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?

1. yi wang says:

Thanks for browsing my blog! I am using WordPress.

33. I have read some excellent stuff here. Certainly worth bookmarking
for revisiting. I wonder how a lot effort you place
to make such a fantastic informative site.

34. Hi, I desire to subscribe for this website to take newest updates, so where can i do it please help out.

1. yi wang says:

Hi Trina, thank you for reading my posts! You may get updates of my blogs through RSS. 🙂

35. 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.

36. Fine way of telling, and good article to obtain data about my presentation subject, which i am going to present in university.

1. yi wang says:

Thank you Rosaline! Good luck on your presentation!

37. 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!

1. yi wang says:

Thank you Spencer!

38. 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

39. Good site you have got here.. It’s difficult to find excellent writing like yours nowadays.
I honestly appreciate individuals like you! Take care!!

1. yi wang says:

Thank you!

40. Hi there, just wanted to tell you, I liked this
post. It was inspiring. Keep on posting!

41. 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
it!

42. 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.

1. yi wang says:

Thank you Myrna!

43. This paragraph offers clear idea designed for the new users of blogging, that genuinely
how to do blogging.

1. yi wang says:

Thank you Bridgette!

44. 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!

45. 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. . . . . .

1. yi wang says:

Thank you Jonathan!

46. 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
post.

1. yi wang says:

Thank you Kavin!

47. Valuable info. Fortunate me I discovered your website
by chance, and I am shocked why this twist of fate didn’t happened
earlier! I bookmarked it.

1. yi wang says:

Thank you!

48. For hottest news you have to pay a visit internet and on web I found this website as a finest
site for latest updates.

1. yi wang says:

Thank you!

49. 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.
Cheers!

1. yi wang says:

Thank you for encouraging me!

50. I got this web page from my friend who informed me
regarding this website and now this time I am browsing
this site and reading very informative content here.

1. yi wang says:

Thank you!

51. Pretty! This has been a really wonderful article.
Thank you for supplying this info.

1. yi wang says:

Thank you Susana!

52. 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!

1. yi wang says:

Thank you Teri!

53. 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
back.

1. yi wang says:

Hi Joanne, thank you for your suggestion! I will improve my posts as you suggested 🙂

54. 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!

1. yi wang says:

Thank you Valeria!

55. Hi friends, its impressive piece of writing
regarding tutoringand completely defined, keep it up all the time.

1. yi wang says:

Thank you Mark!

56. Hi there mates, fastidious article and pleasant urging commented here,
I am actually enjoying by these.

1. yi wang says:

Thank you Jamie!

57. 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!

1. yi wang says:

Hi Alfred, thank you for reading my post! Please feel free to quote them 🙂

58. 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. yi wang says:

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 🙂

59. 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

1. yi wang says:

Thank you!

60. 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. yi wang says:

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 🙂

61. 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.

1. yi wang says:

Thank you for your suggestion! I will be more detailed and clear in my following posts 🙂

62. I am regular reader, how are you everybody? This article
posted at this website is in fact fastidious.

1. yi wang says:

Thank you! I will post more and I hope you will like them 🙂

63. 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.

1. yi wang says:

Thank you!

64. 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.

1. yi wang says:

Thank you for reading my post and inviting me to share my story in your blog! I’d love to do that.

65. 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.

1. yi wang says:

Thank you very much for subscribing my blog!

66. 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!

1. yi wang says:

Thank you!

67. Hello, its good post about media print, we all be familiar with media is a fantastic source of data.

68. 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

69. Federico says:

How would one do this if they do have missing rows.

70. It’s hard to find well-informed people in this particular
topic, however, you seem like you know what you’re talking about!
Thanks

71. Very good written story. It will be beneficial to everyone who employess it, as
well as yours truly :). Keep doing what you are doing

72. As I website possessor I believe the subject material here is real
excellent, thanks for your efforts.

73. Quality posts is the secret to interest the users to pay a quick visit the web site, that’s what this web site is providing.

74. 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

75. 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

76. There’s definately a lot to learn about this topic. I like all of the points you made.

77. 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!

78. Thanks for the auspicious writeup. It if truth be told was once a
amusement account it. Glance complicated to far brought agreeable from you!
By the way, how can we keep in touch?

79. 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.

80. Hi there everybody, here every person is sharing these know-how, so
it’s good to read this blog, and I used to
pay a visit this website every day.

81. 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!

82. I pay a visit everyday a few sites and sites to read posts, except
this website offers quality based content.

83. Hi everybody, here every person is sharing these kinds
of experience, thus it’s nice to read this website, and I used
to go to see this website all the time.

84. Hi there, I want to subscribe for this web site to take most up-to-date updates, so where

85. I do not even understand how I stopped up right here, but I thought this post was good.

I don’t know who you are however definitely you’re going to a famous blogger
if you aren’t already. Cheers!

86. If some one wants expert view on the topic of running a
blog afterward i recommend him/her to pay a quick visit this blog, Keep up the fastidious work.

87. I absolutely love your blog.. Very nice colors & theme.

Did you create this amazing site yourself? Please reply back as I’m wanting to create my own website
and want to find out where you got this from or
just what the theme is called. Appreciate it!

88. I go to see day-to-day some web pages and information sites to read
posts, except this website provides feature based writing.

89. 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.

90. Lovely just what I was searching for. Thanks to the author for taking his clock time on this one.

91. 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.

92. 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.
Cheers!

93. 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.

94. Hi there, I wish for to subscribe for this weblog to obtain newest updates, therefore where can i do it please help.

95. 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

96. Thanks for posting this, it was unbelievably informative and helped me a lot. Well-written blog post.

97. powerballsite says:

I think youve created some actually interesting points. Not also many people would actually think about this the way you just did. 파워볼

98. 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?

99. Very nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyed surfing around your blog posts.
After all I will be subscribing to your feed and I hope you write again soon!