I have data.table as following:-
data <- data.table(k = c("a", "a", "a", "a", "b", "b", "c", "c", "c", "d"),
year = c(2011, 2012, 2013, 2014, 2012, 2013, 2014, 2015, 2016, 2001),
grow_bool = c(1, 1, 0, 1, 0, 1, 1, 0, 1, 0))
# k year grow_bool
# 1: a 2011 1
# 2: a 2012 1
# 3: a 2013 0
# 4: a 2014 1
# 5: b 2012 0
# 6: b 2013 1
# 7: c 2014 1
# 8: c 2015 0
# 9: c 2016 1
#10: d 2001 0
Now, I want to remove the rows which have a 0 in the column grow_bool ans subsequent rows after it for each k. For example:- a in 2013 has a 0 in grow_bool, hence, all this row and all the rows for a after this row should be deleted. All the rows for b will be deleted since, the first row for b consists a 0. d has only one row and it will be deleted since, it has 0.
The resulting data.table should be of the following form:-
data_2 <- data.table(k = c(a, a, c),
year = c(2011, 2012, 2014),
grow_bool = c(1, 1, 1))
# k year grow_bool
# 1: a 2011 1
# 2: a 2012 1
# 3: c 2014 1
Thanks in advance.