0

I am reading a GSheet using R Code.

temp <- read_sheet(url, sheet = "DATA", range = cell_cols("A:N"), col_types = 'c')

The temp variable is a list with a structure like follows:

Col1 Col2 .. Date

I want only get rows where a value in the Date column equals to the current date. Or even before creating temp variable read only rows with the current date.

JohnDilinjer
  • 41
  • 1
  • 7

1 Answers1

2

Since read_sheet already return a tibble table, you can simply apply a filter to the dataframe for rows where the Date is equals the current date (Sys.Date()), like this:

temp <- read_sheet(url, sheet = "DATA", range = cell_cols("A:N"), col_types = 'c')
        %>% dplyr::filter(Date == Sys.Date())

This assumes your Date columns is formated YYYY-MM-DD, if is not, you can use base::format function to format it in a way it matches your data.

Julio GB
  • 427
  • 2
  • 12
  • Hi Julio, I am getting this error Error: Problem with `filter()` input `..1`. x character string is not in a standard unambiguous format i Input `..1` is `"Date" == Sys.Date()`. – JohnDilinjer Oct 06 '20 at 02:14
  • @JohnDilinjer do you mind sharing some information about your data? maybe it has to do with the way your Date column is formated, could be its type is already Date instead of string. try doing ```%>% dplyr::filter(Date == lubridate::ymd(Sys.Date()))``` instead – Julio GB Oct 06 '20 at 02:23
  • also: see this question https://stackoverflow.com/a/40528223/10998856 – Julio GB Oct 06 '20 at 02:29
  • it worked well with lubridate::ymd(Sys.Date())). It was a date type in the GSheet. Thank you very much! – JohnDilinjer Oct 06 '20 at 02:55
  • aslo is it possible to read the rows within an interval i.e. 7 days interval. I am trying to use something like interval(lubridate::ymd(Sys.Date() - 7), lubridate::ymd(Sys.Date())) But it gives an error Input `..1` must be a logical vector, not a Interval. i Input `..1` is `interval(lubridate::ymd(Sys.Date() - 7), lubridate::ymd(Sys.Date()))`. – JohnDilinjer Oct 06 '20 at 04:22
  • you could use two filters ```%>% dplyr::filter(Date >= lubridate::ymd(Sys.Date() - 7)) %>% dplyr::filter(Date <= lubridate::ymd(Sys.Date()))``` – Julio GB Oct 06 '20 at 13:23