r - Count sequences of numbers rowwise -
i have following dataframe 0, 1, , nas ids e on 1 year period:
dat <- data.frame( id = c("a", "b", "c", "d", "e"), jan = c(0, 0, na, 1, 0), feb = c(0, 1, 1, 0, 0), mar = c(0, 0, 1, 0, 1), apr = c(0, na, 0, na, 1), may = c(0, na, 0, 0, 0), jun = c(0, 0, 0, 0, 0), jul = c(0, 0, 0, 0, 1), aug = c(na, 0, 0, 1, 1), sep = c(na, 0, 0, 1, na), okt = c(na, 0, 0, 0, na), nov = c(na, 0, 0, 0, 1), dez = c(na, 0, 0, 0, 0) ) > dat id jan feb mar apr may jun jul aug sep okt nov dez 0 0 0 0 0 0 0 na na na na na b 0 1 0 na na 0 0 0 0 0 0 0 c na 1 1 0 0 0 0 0 0 0 0 0 d 1 0 0 na 0 0 0 1 1 0 0 0 e 0 0 1 1 0 0 1 1 na na 1 0
i count number of 1s each id on 1 year period, following conditions need met:
- the first occurrence of 1 counted 1
- nas should treated 0s
- a second occurrence of 1 counted, if preceded six or more 0s/nas
in example, count be:
> dat id jan feb mar apr may jun jul aug sep okt nov dez count 1 0 0 0 0 0 0 0 na na na na na => 0 2 b 0 1 0 na na 0 0 0 0 0 0 0 => 1 3 c na 1 1 0 0 0 0 0 0 0 0 0 => 1 4 d 1 0 0 na 0 0 0 1 1 0 0 0 => 2 5 e 0 0 1 1 0 0 1 1 na na 1 0 => 1
the function should applied rowwise in form of apply(dat[, -1], 1, my_fun)
, return vector containing count (i.e. 0, 1, 1, 2, 1
). have idea how achieve this?
how using rollapply
zoo package:
library(zoo) library(magrittr) myfun <- function(y, pattern = c(0,0,0,0,0,0,1)){ y[is.na(y)] <- 0 # account both 0s , nas first <- sum(y[1:(length(pattern)-1)])!=0 rest <- y %>% as.numeric() %>% rollapply(7, identical, pattern) %>% sum return(first+rest) } apply(dat[,-1],1,myfun) [1] 0 1 1 2 1
the rollapply part match sequence of 6 0s followed 1 in each row.
the thing left account 1s in first 6 months (which want count won't matched rollapply). done first row of myfun
.
Comments
Post a Comment