Calibrating dplyr filter depends on Sys.Date()

Hi all,
This is my code which filters tweets depends upon Sys.Date().
Problem in this code, after time passes 23:59 that is, new day stars. There will be no tweet or very limited tweets sended by people. So, filter does not give substantial result. My R shiny dashboard does not work.

Is it possible to run this code for example after 12:00? any suggestion?

Thank you all

tweetsday1<- tweetsday %>% dplyr::filter(tweetsday$date==(Sys.Date()))

I try to write if else command like below. However, it does not work. Could you possibly help me?

tweetsday<-todaysdata
tweetsday1<- tweetsday %>% dplyr::filter(tweetsday$date==(Sys.Date()))
row<-nrow(tweetsday1)
row<-as.numeric(row)

checknrow<-function(x) {
if (row <20) {tweetsday1<-tweetsday %>% dplyr::filter(tweetsday$date==(Sys.Date()-1))}
else {tweetsday1<-tweetsday %>% dplyr::filter(tweetsday$date==(Sys.Date()))}
}

checknrow(row)

Hi, can you provide a reproducible example of the dataset?

When using dplyr::filter you don't need tweetsday$, you shoud write it like this:

tweetsday %>% dplyr::filter(date==(Sys.Date()))

Hi @oktayozden
Best option would be to select the last 24 hours instead of working with days.
In my suggestion below, just replace tweets_df with the actual name of your dataframe, and harvested_date with the appropriate variable you have.
Things you may need to check:

  1. make sure your date format is parsed correctly (you may need to adapt ymd_hms)
  2. Check if any issue with time zones
  3. make clear in your UI that you're displaying the last 24 hours
library(tidyverse); library(lubridate)

tweets_df %>% 
  filter(as.double(difftime(now(), ymd_hms(harvested_date), units = "hours")) < 24)

If you need further help, follow the advice of @williaml and share a reprex with code and some actual data.

2 Likes

Thats great! thanks xvalda

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.