Created
November 6, 2014 02:16
-
-
Save mrdwab/6d76ab08a00e45ac0199 to your computer and use it in GitHub Desktop.
Comparing results from different approaches at http://stackoverflow.com/questions/26761849/count-rows-between-nas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dat1 <- data.frame(time = seq(10, 90, 10), | |
value = c(NA, 0, 1, NA, NA, 30, 68, 0, NA)) | |
dat2 <- data.frame(time = seq(10, 90, 10), | |
value = c(0, NA, 1, NA, NA, 30, 68, 0, NA)) | |
RStudent <- function(indf) { | |
aux <- rle(as.numeric((!is.na(indf[, 2])))) | |
cbind(TIME = indf[cumsum(aux$lengths)[which(aux$values == 1)] - | |
aux$lengths[aux$values == 1] +1, 1], | |
Result = rle(is.na(indf$value))$lengths[!rle(is.na(indf$value))$values]) | |
} | |
RStudent(dat1) | |
# TIME Result | |
# [1,] 20 2 | |
# [2,] 60 3 | |
RStudent(dat2) | |
# TIME Result | |
# [1,] 10 1 | |
# [2,] 30 1 | |
# [3,] 60 3 | |
beginneR <- function(indf) { | |
indf %>% | |
group_by(m = cumsum(is.na(value))) %>% | |
summarise(n = n() -1, time = first(time[!is.na(value)])) %>% | |
ungroup() %>% | |
filter(n > 0 & m > 0) %>% | |
select(-m) | |
} | |
beginneR(dat1) | |
# Source: local data frame [2 x 2] | |
# | |
# n time | |
# 1 2 20 | |
# 2 3 60 | |
beginneR(dat2) | |
# Source: local data frame [2 x 2] | |
# | |
# n time | |
# 1 1 30 | |
# 2 3 60 | |
AM <- function(indf) { | |
na.omit(data.table(TIME = indf$time, Val = TrueSeq( | |
!is.na(indf$value), zero2NA = TRUE)))[, list(TIME = TIME[1], .N), by = Val] | |
} | |
AM(dat1) | |
# Val TIME N | |
# 1: 1 20 2 | |
# 2: 2 60 3 | |
AM(dat2) | |
# Val TIME N | |
# 1: 1 10 1 | |
# 2: 2 30 1 | |
# 3: 3 60 3 | |
konvas <- function(indf) { | |
res <- data.frame( | |
time = na.omit(indf$time[which(is.na(indf$value)) + 1]), | |
result = diff(which(is.na(indf$value))) - 1 | |
) | |
res[res$result != 0, ] | |
} | |
konvas(dat1) | |
# time result | |
# 1 20 2 | |
# 3 60 3 | |
konvas(dat2) | |
# time result | |
# 1 30 1 | |
# 3 60 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment