Skip to content

Instantly share code, notes, and snippets.

@TonyLadson
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonyLadson/291a7b7363eb567ed8fd to your computer and use it in GitHub Desktop.
Save TonyLadson/291a7b7363eb567ed8fd to your computer and use it in GitHub Desktop.
Count the number of leading (and trailing) NAs in each column of a data frame
# Usage
# LeadingNA(x)
#
# x is a data frame
# Value
# A data frame, with two rows, leading NAs and trailing NAs
LeadingNA <- function(x){
num.col <- ncol(x)
trailing.na <- rep(0, times = num.col)
leading.na <- rep(0, times = num.col)
for(i in 1:num.col){
my.rl <- rle(is.na(data.cont[[i]]))
if(my.rl$values[1]) leading.na[i] = my.rl$lengths[1]
if(my.rl$values[length(my.rl$values)]) trailing.na[i] = my.rl$lengths[length(my.rl$values)]
}
out <- as.data.frame(rbind(leading.na, trailing.na))
names(out) = names(x)
out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment