How make a filter between specific times?

Hi Community

With this data I'm want to select a filter between 06:00:00 / 18:00:00 and 18:00:00 / 06:00:00.

I'm try this code but don't run well because the second filter don't run well.

Data:https://docs.google.com/spreadsheets/d/1273NoiIeFYaC5F9lZMkRRNj_6Zq-ztC6/edit?usp=sharing&ouid=103209967384892300841&rtpof=true&sd=true

library(tidyverse)

url <- "https://docs.google.com/spreadsheets/d/1273NoiIeFYaC5F9lZMkRRNj_6Zq-ztC6/export?format=xlsx"

download.file(url, "TEMP_Nov2022_Feb2023.xlsx")

TEMP <- readxl::read_xlsx("TEMP_Nov2022_Feb2023.xlsx")

TEMP$DATE <- as.Date(TEMP$DATE,format="%d %m %Y" )

TEMP$DATE<- format(TEMP$DATE, "%Y/%m/%d")

TEMP$TIME <- as.POSIXct(TEMP$TIME, format = "%H:%M:%S")

df_filtered <- TEMP %>% filter(TIME >= as.POSIXct("06:00:00", format = "%H:%M:%S") &
                                  TIME < as.POSIXct("18:00:00", format = "%H:%M:%S")) # run well

df_filtered2 <- TEMP %>% filter(TIME >= as.POSIXct("18:10:00", format = "%H:%M:%S") &
                                 TIME < as.POSIXct("06:00:00", format = "%H:%M:%S")) # run wrong

df_summary <- df_filtered %>% 
  group_by(DATE) %>% 
  summarize(mean_06_18 = mean(TEMPERATURE))

df_summary

df_summary2 <- df_filtered2 %>% 
  group_by(DATE) %>% 
  summarize(mean_18_06 = mean(TEMPERATURE))

df_summary2 # but this not show any values

TEMP_mean <- df_summary |> 
  left_join(df_summary2, by='DATE')

Tnks

I think switching the second filter to OR instead of AND should produce the correct result.

df_filtered2 <- TEMP %>% filter(TIME >= as.POSIXct("18:10:00", format = "%H:%M:%S") |
                                  TIME < as.POSIXct("06:00:00", format = "%H:%M:%S"))
1 Like

Tnks! @scottyd22 , was the solution.

This topic was automatically closed 7 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.