0
votes

Have dataset of 1 year hourly records

for analysis, I need to extract seperately for each month of the year, each hour of the day , so january 00h, january 01h, january 02h, january 03h, ...., ... , march 21h, march 22h, march 23h

Thanks in advance for any useful help!

Select observations for specified hours of the day during a period with datetime, filter, subset, ...

Code below (filter, month (time) generates fatal errror Error: unexpected ')' in "at<-subset(groenenborgerno, timestamp=hour(time) == 01))"

groenenborgerno$timestamp <- as.POSIXct(groenenborgerno$date, format="%Y-%m-%d %H:%M:%S")
library(lubridate)

january01<-filter(atimeframe, 
       (month(time) == 01 & hour(time) == 01) )
1
Have a look at formatSotos
use lubridate to create a variable for each month and a variable for each hour, then you could use group_by(). Would answer if your example was reproducible.heck1

1 Answers

1
votes

Since no data is provided, I will try to answer your question with sample data:

require(lubridate)
require(tidyverse)

## Create some sample data: 
time_index <- seq(from = as.POSIXct("2017-01-01 07:00"), 
                  to = as.POSIXct("2018-01-01 18:00"), by = "hour")
value <- rnorm(n = length(time_index))
data <- data.frame(time_index,value)


data <- data %>% mutate (hour = hour(time_index),
                         month = month(time_index)) %>%
  group_by(month,hour) 

head(data)
> data
# A tibble: 8,772 x 4
# Groups:   month, hour [288]
   time_index           value  hour month
   <dttm>               <dbl> <int> <dbl>
 1 2017-01-01 07:00:00 -0.626     7     1
 2 2017-01-01 08:00:00  0.184     8     1
 3 2017-01-01 09:00:00 -0.836     9     1
 4 2017-01-01 10:00:00  1.60     10     1
 5 2017-01-01 11:00:00  0.330    11     1
 6 2017-01-01 12:00:00 -0.820    12     1
 7 2017-01-01 13:00:00  0.487    13     1
 8 2017-01-01 14:00:00  0.738    14     1
 9 2017-01-01 15:00:00  0.576    15     1
10 2017-01-01 16:00:00 -0.305    16     1
# ... with 8,762 more rows

and then just filter() the hour / month combination you would want like so:

data %>% filter(hour > 12 & month == 1)

# A tibble: 347 x 4
# Groups:   month, hour [11]
   time_index            value  hour month
   <dttm>                <dbl> <int> <dbl>
 1 2017-01-01 13:00:00  0.487     13     1
 2 2017-01-01 14:00:00  0.738     14     1
 3 2017-01-01 15:00:00  0.576     15     1
 4 2017-01-01 16:00:00 -0.305     16     1
 5 2017-01-01 17:00:00  1.51      17     1
 6 2017-01-01 18:00:00  0.390     18     1
 7 2017-01-01 19:00:00 -0.621     19     1
 8 2017-01-01 20:00:00 -2.21      20     1
 9 2017-01-01 21:00:00  1.12      21     1
10 2017-01-01 22:00:00 -0.0449    22     1
# ... with 337 more rows