I have a data frame with a date column titled 'End_Date'. I want to add a new column 'Start_Date' that calculates the day after the previous 'End_Date'. For example:
End_Date <-as.Date(c("2010-11-01", "2010-11-18", "2010-11-26"))
dates <-data.frame(End_Date)
In this example, the new column 'Start Date' to look like this: dates$Start_Date <-as.Date(c(NA, "2010-11-02", "2010-11-19"))
I have tried using sapply, but get an error stating that the new column has one too few rows:
dates$Start_Date <-sapply(2:nrow(dates),
function(i) (dates$End_Date[i]-dates$End_Date[i-1]))
Here, I created a data frame with only 3 rows just as an example, but I need to solution that I can apply to data frames with large numbers of rows.
as.Date(c(NA, (end+1)[-length(end)]), origin = "1970-01-01")
– Rich Scriven