I'm trying to parse a date, but returning error or NAs. I'm using googlesheets4 to import a google sheet. One of the columns has dates in a %d/%m/%Y format, with no leading 0s. Ex: 1/5/2022. I suspect the lack of leading 0s is the problem, but I don't have the option of reformatting the data in the google worksheet (it's a shared document I don't have permision to edit).

My goal is to join this dataframe to another dataframe to compare the dates in both. Thanks in advance!

So far I've tried:

  1. Importing as date with read_sheet. Upon import looks ok, it is recognized as a Date type column and looks like this upon viewing the dataset: "2022-05-01". After joining with the second dataframe, I compare it with the other date and get the following error:

Error in filter():
ℹ In argument: a_datetime > date. Caused by error in as.POSIXlt.character(): ! character string is not in a standard unambiguous format

Code as follows:

library(tidyverse)
library(lubridate)
library(googlesheets4)

first_df <- read_sheet(googleworksheet_id,
                     sheet = sheet_name, 
                     col_types = "iccD") %>% 
  drop_na(date)

second_df <- read_csv("second_df.csv", col_types = cols( a_timedate = col_datetime(format = "%Y-%m-%d %H:%M:%S"), id = col_character()) %>% 
  left_join(first_ds , by = id ) %>% 
  filter(a_datetime > date)
  1. Importing as character with read_sheet and then converting to date, which brings up the following warning:

All formats failed to parse. No formats found.

Code as follows:

library(tidyverse)
library(lubridate)
library(googlesheets4)

first_df <- read_sheet(googleworksheet_id,
                     sheet = sheet_name, 
                     col_types = "iccc") %>% 
  drop_na(date) %>% 
  mutate(date = as_datetime(date, format = '%d/%m/%Y')
0

There are 0 best solutions below