any help would be appreciated!
Essentially, I need a variable that sums the number of previous observations by group while taking account of the date variable.
For Example:
my current data:
ID <- c("A", "A", "A","A", "B", "B", "B")
YEAR <- c(1900, 1901, 1902, 1903, 1900, 1901, 1902)
CASH <- c(1, 2, 3, 1, 0, 1, 0)
DF <- data.frame(ID, YEAR, CASH)
print(DF)
what I would like my data to look like:
ID <- c("A", "A", "A","A", "B", "B", "B")
YEAR <- c(1900, 1901, 1902, 1903, 1900, 1901, 1902)
CASH <- c(1, 2, 3, 1, 0, 1, 0)
PREV_CASH <- c(NA, 1, 3, 6, NA, NA, 1)
DF2 <- data.frame(ID, YEAR, CASH, PREV_CASH)
print(DF2)
I would like to sum the amount of previous cash from the prior year for each group.