There are similarly worded questions, but not quite what I'm looking to do.
I have a data frame where I'd like to remove rows that have n number of consecutive NA values.
1 Manhattan NA 2300 NA 2275 2300
2 Manhattan 2999 2975 NA NA 2995
3 Manhattan NA NA NA NA 4400
4 Manhattan 6725 6500 6750 6995 6800
5 Brooklyn 2000 1995 1975 2000 1999
6 Bronx 1200 3200 NA NA NA
I have some working code that removes n number of total NA values, but would like to tweak it for consecutive.
delete.na <- function(df, n=0) {
df[rowSums(is.na(df)) <= n,]
}
df <- delete.na(df,3)
So, in this case after removing rows with 3 consecutive NA values I would have:
1 Manhattan NA 2300 NA 2275 2300
2 Manhattan 2999 2975 NA NA 2995
4 Manhattan 6725 6500 6750 6995 6800
5 Brooklyn 2000 1995 1975 2000 1999