Consider the following example data frame
df=data.frame(x=c(0,3,5,0,7,6,0),y=c(0,0,3,0,0,4,0),z=c(8,7,6,8,9,4,3))
I want to remove the first and last rows, where y=0, that is row 1,2 and 7 - without removing row 4 and 5.
I can filter out any row with zero using filter(!y==0)
and can see slice_head()
and slice_tail()
recommended for deleting based on location (n=). I am looking for a way to conditionally remove head and tail based on y.
The full dataset consists of 200,000 rows with data collected across dates and id's. I will be applying this per day and id, using group_by(id,date). The length of the head and tail with zeros varries across dates, thus I cannot use slice_head(n=2)
.
I am working in tidyverse (mainly/so far).
Thanks in advance :)
Position
, cheers! – Teresetereshkova