I would like to slice the head of my dataframe after the first time TRUE shows up in a certain column.
dataframe <- data.frame(C1 = c(4.32, 8.4, 6.43, 7.98, 5.68, 4.97), C2 = c(4.32, 9.87, 5.43, 6.54, 2.34, 6.59), Logical = c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE))
That the output would look like this:
dataframe_new <- data.frame(C1 = c( 7.98, 5.68, 4.97), C2 = c(6.54, 2.34, 6.59), Logical = c(FALSE, TRUE, FALSE))
Thanks in advance!
It would be great if someone knew how to do it in a list as well...
CodePudding user response:
Here is a base R approach:
which(dataframe$Logical == T)[1]returns the row index of the first occurrence ofTRUEin theLogicalcolumn(which(dataframe$Logical == T)[1] 1)therefore specifies one row after the firstTRUE(which(dataframe$Logical == T)[1] 1):nrow(dataframe)therefore output rows starting from one row after the first occurrence ofTRUEto the number of row ofdataframe, that is the end ofdataframe
dataframe[(which(dataframe$Logical == T)[1] 1):nrow(dataframe), ]
C1 C2 Logical
1 7.98 6.54 FALSE
2 5.68 2.34 TRUE
3 4.97 6.59 FALSE
CodePudding user response:
You could use cumsum to get every rows after the first TRUE value (including the first TRUE value), and then use tail(., -1) to remove the first TRUE.
tail(dataframe[cumsum(dataframe$Logical) >= 1,], -1)
C1 C2 Logical
4 7.98 6.54 FALSE
5 5.68 2.34 TRUE
6 4.97 6.59 FALSE
Another solution is to use cumany which by default set every value after the first TRUE to TRUE.
library(dplyr)
dataframe[complete.cases(cumany(lag(dataframe$Logical))),]
