I have a dataframe with several columns. One column (C3) includes a marker which is defined by certain pattern of values between the range of 8 and 10 for 50 or more rows (marker in example only with 4 rows). Those values could be found spreaded in the rest of the dataframe as well (also more than 50 times if summed up), but only in the marker it's found in consecutive lines for 50 times or more.
data <- data.frame(C1 = c(12.3, 5.2, 2.43, 6, 8.1, 1, 3.25, 3.67, 12.87, NA, 2, 4.5),
C2 = c(NA, 7, 2.5, 4.53, 3.5, NA, 9, 9.75, 10.4, NA, 3, NA),
C3 = c(3.41, 5.3, 8.1, 9.5, 9.99, 8.45, 7.2, 6.8, 5.4, 3.2, NA, 9.3),
C4 = c(10.54, 2.3, 8.4, 9.32, 3.65, 5.8, NA, 4.5, 7.6, NA, 6.7, 5.6),
C5 = c(3.45, 6.97, 5, 9.87, 2, 3.4, 5.6, 9.8, NA, NA, 0.3, 11.5))
I would like to start a new dataframe for calculations at the end of the marker + 5 more rows to be really sure about "clean" data without any marker leftovers. Those markers can be found in the column at random places and I always want to start there with the new dataframe. That in the end my new dataframe should be like this...
data_new <- data.frame(C1 = c(3.25, 3.67, 9.87, NA, 2, 4.5),
C2 = c(9, 9.75, 10.4, NA, 3, NA),
C3 = c(7.2, 6.8, 5.4, 3.2, NA, 9.3),
C4 = c(NA, 4.5, 7.6, NA, 6.7, 5.6),
C5 = c(5.6, 9.8, NA, NA, 0.3, 11.5))
So that the beginning of the old dataframe inclusive marker are cut away.
I found a solution meanwhile without a loop...
data <- add_column(data, Filter = data$C3 > 8 & data$C3 < 10, .after = "C3")
r <- rle(data$Filter)
test <- data.frame(values = r$values, lengths = r$lengths)
test$ID <- 1:nrow(test)
marker <- as.numeric((min(which(test$values == TRUE & test$lengths >= 4)))) # 4.Stelle entziffert
data_drop <- test[c(1:marker),]
data_drop_c <- as.numeric(sum(data_drop$lengths))
final_df <- data[-c(1:data_drop_c), ]
Nevertheless many thanks for trying!