I am having difficulty attempting to figure out how to pull all of the rows from a data frame before an occurrence. I know I can pull the first occurrence of something quite easily, but for my specific case I need to pull all cases before the specified. It is also a little trickier because they are separated into customers, so it has to be according to each customer. Some of the customers also have multiple positive occurrences.
So far I have been able to almost achieve this using for loops but there are a few incorrect rows being pulled.
Essentially I have two data sets, one where the positive case occurs only once and another where it occurs frequently.
Data set where positive case occurs only once.
data.frame(ID = c("103", "103", "103", "103", "103", "107", "107", "107", "107", "107"),
Items = c("638,193,1937,193", "3918,38327,1938,200", "860", "3982,392,3019,3928", "4038,291,493,029,192", "3604,1361,453,2782", "117", "860", "291", "203"),
rank = c(1,2,3,4,5,1,2,3,4,5),
Ordercount = c(0,0,1,0,0,0,0,1,0,0))
Data set where positive case occurs more than once.
data.frame(ID = c("103", "103", "103", "103", "103", "107", "107", "107", "107", "107"),
Items = c("638,193,1937,193", "3918,38327,1938,200", "860", "3982,392,3019,3928", "4038,291,493,029,192", "3604,1361,453,2782", "117", "860", "291", "203"),
rank = c(1,2,3,4,5,1,2,3,4,5),
Ordercount = c(0,0,1,0,1,0,0,0,1,1))
Desired Ouput
#First Case
data.frame(ID = c("103", "103","107", "107"),
Items = c("638,193,1937,193", "3918,38327,1938,200","3604,1361,453,2782", "117"),
rank = c(1,2,1,2),
Ordercount = c(0,0,0,0))
ID Items rank Ordercount
1 103 638,193,1937,193 1 0
2 103 3918,38327,1938,200 2 0
3 107 3604,1361,453,2782 1 0
4 107 117 2 0
# Second Case
data.frame(ID = c("103", "103","107", "107", "107"),
Items = c("638,193,1937,193", "3918,38327,1938,200","3604,1361,453,2782", "117", "860"),
rank = c(1,2,1,2,3),
Ordercount = c(0,0,0,0,0))
ID Items rank Ordercount
1 103 638,193,1937,193 1 0
2 103 3918,38327,1938,200 2 0
3 107 3604,1361,453,2782 1 0
4 107 117 2 0
5 107 860 3 0