I have a dataframe in a database with a date dimension that I pulled using RPostgres. Each 'date' is in the format 'YYYY-MM-DD. I'd like to add a new column of dates (labeled 'lookback_date') that are one year earlier than the initial dates.
To be clear, if an observation's 'date' were '2000-01-01', I'd want to add a new 'lookback_date' of '1999-01-01' to that observation. Unfortunately, I can't figure out how to do that. Normally, I'd use Lubridate, but, from what I can tell, it doesn't work with dbplyr. Here's a streamlined version of my code so far. Everything in my actual code works fine up until the mutate function.
# Packages
library(dbplyr)
library(RPostgres)
# Connect to db
drv <- dbDriver("Postgres")
# Setup connect to db
conn <- dbConnect(drv,
dbname = etc,)
# Define table to use in db
table <- tbl(conn, in_schema("xyz", "abc"))
#Select columns and filter
base_data <- table %>%
#Filter for pertinent data
filter(date > as.Date("2018-01-01") & date <= as.Date("2020-01-01"))
modified_data <- base_data %>%
mutate(lookback_date = date - 365)
Is there another way I can create this new column of dates?
Thanks!