Noura
Noura

Reputation: 474

Count number of occurrence of zero between non-zero value in R

I have a matrix

mat <- matrix(c(64,76,0,0,78,35,45,0,0,4,37,0,66,46,0,0,0,0,3,0,71,0,28,97,0,30,55,65,116,30,18,0,0,143,99,0,0,0,0,0), nrow=4, byrow=T)

mat
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   64   76    0    0   78   35   45    0    0     4
[2,]   37    0   66   46    0    0    0    0    3     0
[3,]   71    0   28   97    0   30   55   65  116    30
[4,]   18    0    0  143   99    0    0    0    0     0

I want to create a list which count the number of occurrence of zero between non-zero value

[[1]]
[1] 2 2

[[2]]
[1] 1 4 1

[[3]]
[1] 1 1

[[4]]
[1] 2 5

Upvotes: 2

Views: 228

Answers (4)

IceCreamToucan
IceCreamToucan

Reputation: 28685

If for some reason you have a matrix with many rows and you need to do this a few seconds faster (unlikely I know), you can use the method below

library(dplyr)

rle(c(t(mat))) %>% 
  do.call(what = data.frame) %>% 
  mutate(mrow = (cumsum(lengths) - 1) %/% ncol(mat)) %>% 
  {split(.$lengths[!.$values], .$mrow[!.$values])}

# $`0`
# [1] 2 2
# 
# $`1`
# [1] 1 4 1
# 
# $`2`
# [1] 1 1
# 
# $`3`
# [1] 2 5

Benchmark

mat <- mat[sample(nrow(mat), 1e6, T),]

f1 <- function(mat){
  apply(mat, 1, function(x) {
   with(rle(x), lengths[values == 0])
 })
}
f2 <- function(mat){
rle(c(t(mat))) %>% 
  do.call(what = data.frame) %>% 
  mutate(mrow = (cumsum(lengths) - 1) %/% ncol(mat)) %>% 
  {split(.$lengths[!.$values], .$mrow[!.$values])}
}

microbenchmark::microbenchmark(f1(mat), f2(mat), times = 10)
# Unit: seconds
#     expr       min        lq      mean    median        uq       max neval
#  f1(mat) 28.346335 28.978307 30.633423 30.720702 31.504075 35.049800    10
#  f2(mat)  3.683452  3.916681  4.099936  4.086634  4.250613  4.482668    10

Upvotes: 0

d.b
d.b

Reputation: 32548

One more

setNames(object = lapply(X = data.frame(t(mat)),
                         FUN = function(x)
                             with(rle(x == 0), lengths[values])),
         nm = NULL)
#[[1]]
#[1] 2 2

#[[2]]
#[1] 1 4 1

#[[3]]
#[1] 1 1

#[[4]]
#[1] 2 5

Upvotes: 1

Sonny
Sonny

Reputation: 3183

All you need is rle

> apply(mat, 1, function(x) {
   rle(x)$length[rle(x)$values == 0]
 })
[[1]]
[1] 2 2

[[2]]
[1] 1 4 1

[[3]]
[1] 1 1

[[4]]
[1] 2 5

Upvotes: 6

Sada93
Sada93

Reputation: 2835

You can use rle which calculates the number of consecutive numbers

mat <- matrix(c(64,76,0,0,78,35,45,0,0,4,37,0,66,46,0,0,0,0,3,0,71,0,28,97,0,30,55,65,116,30,18,0,0,143,99,0,0,0,0,0), nrow=4, byrow=T)
apply(mat,1,function(x) {
  value = rle(x==0)
  value$length[value$values]
  })

Upvotes: 2

Related Questions