Subset data by hours

I used the following code for subsetting data by date, and that works. But is there an equally simple way to subset data by hours into a new data frame? Sure this requires some parsing with lubridate.

df2 <- df %>%
  filter(Time >= as.Date('2020-04-05') & Time <= as.Date('2020-04-06'))

I have used this practice data

df <- structure(list(Date = c("2.5.2012", "2.5.2012", "2.5.2012", "2.5.2012", 
"3.5.2012", "3.5.2012", "3.5.2012", "3.5.2012", "3.5.2012", "4.5.2012", 
"4.5.2012", "5.5.2012", "5.5.2012", "5.5.2012", "5.5.2012", "5.5.2012", 
"6.5.2012", "6.5.2012", "7.5.2012", "7.5.2012", "8.5.2012", "8.5.2012", 
"8.5.2012", "8.5.2012", "8.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", 
"9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", 
"9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "10.5.2012", 
"10.5.2012", "10.5.2012", "10.5.2012", "10.5.2012", "10.5.2012", 
"11.5.2012", "11.5.2012", "11.5.2012", "12.5.2012", "12.5.2012", 
"14.5.2012", "14.5.2012", "14.5.2012", "15.5.2012", "15.5.2012", 
"15.5.2012", "15.5.2012", "15.5.2012", "16.5.2012", "16.5.2012", 
"17.5.2012", "17.5.2012", "17.5.2012", "17.5.2012", "17.5.2012", 
"18.5.2012", "18.5.2012", "18.5.2012", "18.5.2012", "18.5.2012", 
"19.5.2012", "20.5.2012", "20.5.2012", "20.5.2012", "20.5.2012", 
"21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", 
"21.5.2012", "21.5.2012", "21.5.2012", "22.5.2012", "22.5.2012", 
"22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", 
"22.5.2012", "22.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", 
"23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", 
"23.5.2012"), Time = c("07:30", "17:20", "18:40", "19:40", "09:30", 
"09:40", "18:00", "16:30", "18:30", "13:50", "09:00", "12:20", 
"09:20", "09:00", "12:20", "20:10", "11:20", "08:10", "12:20", 
"13:30", "10:00", "20:40", "10:40", "12:50", "20:30", "09:30", 
"13:40", "10:30", "10:20", "13:00", "13:30", "10:40", "14:10", 
"12:40", "14:40", "16:20", "16:10", "22:40", "08:40", "13:40", 
"12:30", "14:20", "16:30", "15:00", "10:50", "09:40", "20:00", 
"12:30", "09:20", "13:10", "13:10", "08:00", "14:00", "19:30", 
"17:50", "16:30", "19:40", "12:40", "20:00", "07:20", "10:20", 
"07:30", "15:30", "20:00", "08:00", "08:50", "10:40", "12:00", 
"12:20", "16:30", "09:00", "20:50", "17:40", "18:50", "08:30", 
"13:00", "10:00", "16:20", "18:40", "19:20", "19:20", "19:40", 
"19:10", "11:30", "09:10", "10:10", "13:20", "15:20", "16:30", 
"19:30", "20:00", "09:00", "11:50", "09:00", "12:00", "13:00", 
"09:00", "14:10", "10:30", "13:20")), row.names = c(NA, -100L
), class = c("tbl_df", "tbl", "data.frame"))
suppressPackageStartupMessages({
  library(dplyr)
  library(lubridate)
  library(magrittr)
})

the_dates <- data.frame(
  Date_part =
    c("2.5.2012", "2.5.2012", "2.5.2012", "2.5.2012", "3.5.2012", "3.5.2012", "3.5.2012", "3.5.2012", "3.5.2012", "4.5.2012", "4.5.2012", "5.5.2012", "5.5.2012", "5.5.2012", "5.5.2012", "5.5.2012", "6.5.2012", "6.5.2012", "7.5.2012", "7.5.2012", "8.5.2012", "8.5.2012", "8.5.2012", "8.5.2012", "8.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "9.5.2012", "10.5.2012", "10.5.2012", "10.5.2012", "10.5.2012", "10.5.2012", "10.5.2012", "11.5.2012", "11.5.2012", "11.5.2012", "12.5.2012", "12.5.2012", "14.5.2012", "14.5.2012", "14.5.2012", "15.5.2012", "15.5.2012", "15.5.2012", "15.5.2012", "15.5.2012", "16.5.2012", "16.5.2012", "17.5.2012", "17.5.2012", "17.5.2012", "17.5.2012", "17.5.2012", "18.5.2012", "18.5.2012", "18.5.2012", "18.5.2012", "18.5.2012", "19.5.2012", "20.5.2012", "20.5.2012", "20.5.2012", "20.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "21.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "22.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012", "23.5.2012"),
  Time_part =
    c("07:30", "17:20", "18:40", "19:40", "09:30", "09:40", "18:00", "16:30", "18:30", "13:50", "09:00", "12:20", "09:20", "09:00", "12:20", "20:10", "11:20", "08:10", "12:20", "13:30", "10:00", "20:40", "10:40", "12:50", "20:30", "09:30", "13:40", "10:30", "10:20", "13:00", "13:30", "10:40", "14:10", "12:40", "14:40", "16:20", "16:10", "22:40", "08:40", "13:40", "12:30", "14:20", "16:30", "15:00", "10:50", "09:40", "20:00", "12:30", "09:20", "13:10", "13:10", "08:00", "14:00", "19:30", "17:50", "16:30", "19:40", "12:40", "20:00", "07:20", "10:20", "07:30", "15:30", "20:00", "08:00", "08:50", "10:40", "12:00", "12:20", "16:30", "09:00", "20:50", "17:40", "18:50", "08:30", "13:00", "10:00", "16:20", "18:40", "19:20", "19:20", "19:40", "19:10", "11:30", "09:10", "10:10", "13:20", "15:20", "16:30", "19:30", "20:00", "09:00"
      , "11:50", "09:00", "12:00", "13:00", "09:00", "14:10", "10:30", "13:20"))

the_dates %<>% 
  mutate(full_datetime = dmy_hm(paste(Date_part,Time_part,sep = " ")))
  

the_dates %>% filter(hour(full_datetime) == 9)
#>    Date_part Time_part       full_datetime
#> 1   3.5.2012     09:30 2012-05-03 09:30:00
#> 2   3.5.2012     09:40 2012-05-03 09:40:00
#> 3   4.5.2012     09:00 2012-05-04 09:00:00
#> 4   5.5.2012     09:20 2012-05-05 09:20:00
#> 5   5.5.2012     09:00 2012-05-05 09:00:00
#> 6   9.5.2012     09:30 2012-05-09 09:30:00
#> 7  11.5.2012     09:40 2012-05-11 09:40:00
#> 8  12.5.2012     09:20 2012-05-12 09:20:00
#> 9  20.5.2012     09:00 2012-05-20 09:00:00
#> 10 22.5.2012     09:10 2012-05-22 09:10:00
#> 11 23.5.2012     09:00 2012-05-23 09:00:00
#> 12 23.5.2012     09:00 2012-05-23 09:00:00
#> 13 23.5.2012     09:00 2012-05-23 09:00:00
1 Like

Thanks technocrat! I get this cryptic error message → Error: unexpected ',' in " ," when I try to make that Time_part data frame

One of the beauties of reprex is that it should always work unless there are missing libraries or libraries that need updating badly.

Try cut-and-pasting to make sure no transcription errors were introduced.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.