I have a data set of different IDs, Date, Quantities. All IDs have the same exact start date, however some have starting quantities of 0 so basically the start date should be later.
I am trying to filter the rows such that each ID
df <- data.frame(ID = c("1", "1", "1", "1", "1", "1",
"2", "2", "2", "2", "2", "2",
"3", "3", "3", "3", "3", "3"),
Date = c(seq(as.Date("2000/1/1"), by = "month", length.out = 6),
seq(as.Date("2000/1/1"), by = "month", length.out = 6),
seq(as.Date("2000/1/1"), by = "month", length.out = 6)),
QTY = c(0, 0, 0, 40, 0, 60, 50, 55, 0, 70, 88, 64, 0, 43, 43, 0, 99, 99))
So basically in the df example above, I want ID number 1 to start from the fourth QTY value, and for ID number 3 to start from the second QTY value.
I tried using group by and to mutate a new column with an ifelse but I had some error.
My expected df should be a transformation from the first one to the one below, so just to remove the first rows with zeroes for each ID until QTY starts to be more than 0
df <- data.frame(ID = c("1", "1", "1",
"2", "2", "2", "2", "2", "2",
"3", "3", "3", "3", "3"),
Date = c(seq(as.Date("2000/4/1"), by = "month", length.out = 3),
seq(as.Date("2000/1/1"), by = "month", length.out = 6),
seq(as.Date("2000/2/1"), by = "month", length.out = 5)),
QTY = c(40, 0, 60, 50, 55, 0, 70, 88, 64, 43, 43, 0, 99, 99))