I have a df with 1000s of rows. Simplified it looks like this:
df1 <- data.frame(Sample = c('A1', 'B2', 'C3', 'A1', 'B2', 'C3','A1', 'B2', 'C3'),
Day = c('1', '3', '5', '4', '2', '6','5', '4', '2' ),
Value = c('0.1', '0.8', '0.7', '0.6', '0.3', '0.4', '0.4', '0.2', '0.3'))
I want to remove samples which occur on days after the max value was measured.
I can look at the max value and which days they fall on for each sample:
library(dplyr)
df2 <- df1 %>%
group_by(Value, Day, Sample) %>%
slice(which.max(Value))
Then if I could remove rows for each sample which occurs on days after the maximum was measured I'd have the desired df:
df3 <- data.frame(Sample = c('A1', 'B2', 'C3', 'A1', 'B2', 'C3'),
Day = c('1', '3', '5', '4', '2','2' ),
Value = c('0.1', '0.8', '0.7', '0.6', '0.3', '0.3'))
This seems like it needs a for-loop, but I'm not firing on many cylinders today, so any help is much appreciated. TIA.