Home > database >  how to filter by date and time?
how to filter by date and time?

Time:01-13

Hi everyone I am new to using R and I need help with a database. Currently, I am making a filter by date but in addition to this, I need that each date has a filter between hours ie for example, that the information shown is 2019-10-31 between 18:00:00:00 and 06:00:00:00 of that day, the information of 2019-11-1 is also between the time intervals mentioned above and the same for the following days until 2020-01-12. I do not know if this is possible or if I have to make some additional steps I hope you can help me with this doubt.

Here is the code that I'm using

data2<-M03100000246%>%
  mutate(Time=ymd_hms(Date_Measure),
         Date_Measure=floor_date(Time,unit = "hour"))%>%
  group_by(Date_Measure)

hourly246<-data2%>%
  summarise(Id="M03100000246",Tmin=min(T_atm_C,na.rm = T),
            Tmax=max(T_atm_C,na.rm = T),
            Tmedia=median(T_atm_C,na.rm = T),
            Smin=min(SR_W_m2,na.rm = T),
            Smax=max(SR_W_m2,na.rm = T),
            Smedia=median(SR_W_m2,na.rm = T),
            Hrmin=min(HR,na.rm = T),
            Hrmax=max(HR,na.rm = T),
            Hrmedia=median(HR,na.rm = T))%>%
  ungroup()%>%
  data.frame()


SD_HD246<-hourly246%>%
  filter(Date_Measure>="2019-10-31" & Date_Measure<"2020-01-12")%>%
  filter(Date_Measure>="18:00:00" & Date_Measure<="06:00:00")%>%
  as.data.frame()

but when I run the last one receive this error

Error: Problem with filter() input ..1. i Input ..1 is Date_Measure >= "18:00:00" & Date_Measure <= "06:00:00". x character string is not in a standard unambiguous format Run rlang::last_error() to see where the error occurred.

CodePudding user response:

You need to extract the time for the filtering on time. First fix:

filter(hour(Date_Measure) >= 18 & hour(Date_Measure) <= 6)

Next problem: a number can't be >= 18 and <= 6. You need or here. (Assuming you want night times. If you want day times, keep the & and switch your <= and `>=.) Second fix:

filter(hour(Date_Measure) >= 18 | hour(Date_Measure) <= 6)
  •  Tags:  
  • Related