Skip to content

Instantly share code, notes, and snippets.

@MCMaurer
Created December 14, 2018 23:06
Show Gist options
  • Save MCMaurer/6d821fa5643cf0791a92d29672b8edf2 to your computer and use it in GitHub Desktop.
Save MCMaurer/6d821fa5643cf0791a92d29672b8edf2 to your computer and use it in GitHub Desktop.
Find all the rows in a dataframe where a certain sequence appears at any point
result <- matrix(nrow = nrow(mtcars), ncol = ncol(mtcars)-1)
for (i in 1:32) {
result[i,] <- rollapply(as.character(mtcars[i,]), 2, identical, c("1", "0"))
}
result2 <- vector(length = nrow(mtcars))
for (i in 1:32) {
result2[i] <- TRUE %in% result[i,]
}
mtcars[result2,]
# for the rows where you DON'T have that sequence
mtcars[!result2,]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment