{"id":289,"date":"2017-05-04T11:49:12","date_gmt":"2017-05-04T18:49:12","guid":{"rendered":"https:\/\/blogs.ubc.ca\/yiwang28\/?p=289"},"modified":"2017-05-04T11:49:12","modified_gmt":"2017-05-04T18:49:12","slug":"my-r-learning-notes-quick-ways-to-aggregate-minutely-data-into-hourly-data","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/yiwang28\/2017\/05\/04\/my-r-learning-notes-quick-ways-to-aggregate-minutely-data-into-hourly-data\/","title":{"rendered":"My R learning notes: quick ways to aggregate minutely data into hourly data"},"content":{"rendered":"<p>Recently I ran into a stone wall when I tried to analyse the time series data from my first-year experiments.<\/p>\n<p>In my experiments, I measured tree sap flow velocity and different environmental controls such as temperature, solar radiation, relative humidity, wind speed and soil moisture. However, these data were at different time steps. The sap flow velocity were at a hourly basis, while the environmental controls were measured at either every 10 minutes or 20 minutes. So I had to aggregate those into the same time sequences.<\/p>\n<p>There are two types of aggregating data. The first is to assemble these data at every hour at each date, in order to make a correlation analysis or multiple linear regression; and the second is to average data which are measured at the same hour, disregarding the date, aiming at analyzing the 24-hour profile of the variables.<\/p>\n<p>Thanks to R, I succeeded in dealing with my four-month data at just ten seconds. Now, I&#8217;d like to share the codes with you, giving my soil moisture data as an example. \u00a0\ud83d\ude42<\/p>\n<ol>\n<li>\n<h6><span style=\"color: #ff6600;\">Making a 24-hour profile of soil moisture<\/span><\/h6>\n<\/li>\n<\/ol>\n<p style=\"padding-left: 30px;\">Here is part of my soil moisture data (unit: VW%) at my Treatment 2 plot. I measured the soil moisture at two depth: Shallow ( at the 20 cm depth), and Deep (at the 40 cm depth).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-297 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/2.jpg\" alt=\"\" width=\"332\" height=\"419\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/2.jpg 332w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/2-238x300.jpg 238w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/p>\n<p style=\"text-align: center;\">Fig. 1 Example of initial soil moisture data<\/p>\n<p style=\"text-align: left; padding-left: 30px;\">(1) Save the excel file as *.csv, and import it into R Studio. In my case, I input my data as data frame named soilt2. Please remember to change the data type of Datetime column as Datetime, and turn Shallow column and Deep colunm as numeric.<\/p>\n<p style=\"text-align: left; padding-left: 30px;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-299 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/3.jpg\" alt=\"\" width=\"1189\" height=\"572\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/3.jpg 1189w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/3-300x144.jpg 300w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/3-768x369.jpg 768w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/3-1024x493.jpg 1024w\" sizes=\"auto, (max-width: 1189px) 100vw, 1189px\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: center;\">Fig. 2 Input soil moisture dataset<\/p>\n<p style=\"padding-left: 30px; text-align: left;\">(2) Input code:<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><span style=\"color: #333399;\">soilt2$Datetime1 &lt;- as.POSIXlt(soilt2$Datetime)$hour<\/span> \u00a0### This will add a new colunm named Datetime1 in the soilt2 dataset. Datetime1 displayed below showed that only the hour was extracted from Datetime.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-300 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/5.jpg\" alt=\"\" width=\"395\" height=\"319\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/5.jpg 395w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/5-300x242.jpg 300w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: center;\">Fig. 3 the added new colunm Datetime1 is showed on the right of the table.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><span style=\"color: #333399;\">soilt2 &lt;- aggregate(cbind(Shallow, Deep) ~ Datetime1, data=soilt2, FUN=mean)<\/span> ### This averaged Shallow and Deep data into hourly data, \u00a0regardless of the date. In my case, I changed my original dataset. If you don&#8217;t want to make change of the original dataset, please give a new name to your new dataset, such as soilt3.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-301 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/6.jpg\" alt=\"\" width=\"289\" height=\"328\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/6.jpg 289w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/6-264x300.jpg 264w\" sizes=\"auto, (max-width: 289px) 100vw, 289px\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: center;\">Fig. 4 The changed soilt2 data frame.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\">Done! Yay!<\/p>\n<p style=\"padding-left: 30px; text-align: left;\">\n<h6 style=\"padding-left: 30px; text-align: left;\">2<span style=\"color: #ff6600;\">. Assemble the data at each hour at each date.<\/span><\/h6>\n<p style=\"padding-left: 30px; text-align: left;\">(1) Import the dataset (the same as the first method).<\/p>\n<p style=\"padding-left: 30px; text-align: left;\">(2) Input the following code<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><span style=\"color: #333399;\">soilt2$Datetime2 &lt;- droplevels(cut(soilt2$Datetime, breaks=&#8221;hour&#8221;))<\/span> ### This added a new colunm named Datetime2 in soilt2, which includes the date and the hour extracted from Datetime column.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-302 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/7.jpg\" alt=\"\" width=\"469\" height=\"298\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/7.jpg 469w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/7-300x191.jpg 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: center;\">Fig. 5 The added new column Datetime2 is showed on the right of the table.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><span style=\"color: #333399;\">soilt3 &lt;- aggregate(cbind(Shallow, Deep) ~ Datetime2, data=soilt2, FUN=mean)<\/span> ### This calculate the mean of data in each hour at each date.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-303 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/8.jpg\" alt=\"\" width=\"325\" height=\"294\" srcset=\"https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/8.jpg 325w, https:\/\/blogs.ubc.ca\/yiwang28\/files\/2017\/05\/8-300x271.jpg 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/p>\n<p style=\"padding-left: 30px; text-align: center;\">Fig. 6 The new soilt3 data frame.<\/p>\n<p style=\"padding-left: 30px; text-align: left;\">Yay again! Done!<\/p>\n<p style=\"padding-left: 30px;\">If you want to output these data into excel, you can load package (xlsx), and using the code of write.xlsx( \u00a0). \u00a0For example, I use the following code:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #333399;\">write.xlsx(soilc1, &#8220;c:\/Users\/Yi Wang\/Desktop\/soilc1.xlsx&#8221;)<\/span><\/p>\n<p>There are definitely other ways to aggregate the data, but the codes that I put on this blog, in my opinion, is the most simple code. If you disagree with me, or you&#8217;d like to share your code, please feel free to leave your comments.<\/p>\n<p>Thank you for reading!<\/p>\n<p>May, 04, 2017<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I ran into a stone wall when I tried to analyse the time series data from my first-year experiments. In my experiments, I measured tree sap flow velocity and different environmental controls such as temperature, solar radiation, relative humidity, wind speed and soil moisture. However, these data were at different time steps. The sap &hellip; <a href=\"https:\/\/blogs.ubc.ca\/yiwang28\/2017\/05\/04\/my-r-learning-notes-quick-ways-to-aggregate-minutely-data-into-hourly-data\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">My R learning notes: quick ways to aggregate minutely data into hourly data<\/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-289","post","type-post","status-publish","format-standard","hentry","category-blogs"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/289","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=289"}],"version-history":[{"count":8,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/posts\/289\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/yiwang28\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}