Skip to main content
Código corrigido.
Fonte Link
Rui Barradas
  • 19mil
  • 2
  • 14
  • 28
x<-'1   2   3   4   5   6   7  apply(dados, 8
1   10  5   0   0   25  12, \(x) 0{
2   0   0   0   0   0   0 r <- 1rle(x)
3   40sum(r$lengths[r$values == 250] > 5   0   1   0   32)
4   0   10  6   3   0   0   2})
5   2 #> [1] 0  1 0   0   3   0   0
6   10  3   15  30  3   0   01
7   0   0   0   0  

Created on 2022-08-18 by the reprex package (v2.0.1)


Para obter as sequências de valores menores que ou iguais a 1 é um bocado mais complicado. Mas também pode ser resolvido com um ciclo apply, basta primeiro substituir os 1 por 0 e repetir o código acima.

apply(dados2, 01, \(x) {
 0 x[x <= 18'
dados1] <- read.table(textConnection(x), header = TRUE, check.names0
 = FALSE)

r <- apply(dados, 1, rle)
sapply(r, \(x)
  sum(x$lengths[x$valuesr$lengths[r$values == 0] > 5)
})
#> [1] 0 1 0 0 01 0 1

Created on 2022-0810-18 by the19 with reprex packagev2.0.2 (v2.0.1)

Dados em formato dput

dados <x<-'1   2   3   4   5   6   7   8
1  structure(list( 10  5   0   0   25  12  0
2   0 `1` = 0   0   0   0   0   1:7,
3   40  25  5   0   1   0   32
4   0 `2` = c(10L,10 0L, 40L,6 0L, 2L, 10L,3 0L),  0   0   2
5   2 `3` = c(5L,0 0L, 25L, 10L,0 0L, 3L, 0L),0   3   0   0
6   10 `4` =3 c(0L, 0L, 5L,15 6L, 0L,30 15L, 0L),3   0   0
7   0 `5` = c0   0   0   0   0   18'
dados <- read.table(0LtextConnection(x), 0L,header 0L,= 3LTRUE, 0L,check.names 30L,= 0LFALSE),

y<-'1   2   3   4   5   6   7   8
1   10 `6` =5 c(25L, 0L, 1L,0 0L, 3L, 3L,0 0L),  25  12  0
2   0 `7` = c(12L,0 0L, 0L, 0L,0 0L, 0L, 0L),0   0   0   1
3   40 `8` =25 c(0L, 1L,5 32L, 2L, 0L,0 0L, 18L)), 1   0   32
4   0 class = "data10  6   3   0   0   2
5   2   0   0   0   1   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados2 <- read.frame"table(textConnection(y), row.namesheader = c(NATRUE, -7L)check.names = FALSE)
x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados <- read.table(textConnection(x), header = TRUE, check.names = FALSE)

r <- apply(dados, 1, rle)
sapply(r, \(x) sum(x$lengths[x$values == 0] > 5))
#> [1] 0 1 0 0 0 0 1

Created on 2022-08-18 by the reprex package (v2.0.1)

Dados em formato dput

dados <-
  structure(list(
    `1` = 1:7, 
    `2` = c(10L, 0L, 40L, 0L, 2L, 10L, 0L), 
    `3` = c(5L, 0L, 25L, 10L, 0L, 3L, 0L), 
    `4` = c(0L, 0L, 5L, 6L, 0L, 15L, 0L), 
    `5` = c(0L, 0L, 0L, 3L, 0L, 30L, 0L), 
    `6` = c(25L, 0L, 1L, 0L, 3L, 3L, 0L), 
    `7` = c(12L, 0L, 0L, 0L, 0L, 0L, 0L), 
    `8` = c(0L, 1L, 32L, 2L, 0L, 0L, 18L)), 
    class = "data.frame", row.names = c(NA, -7L))
apply(dados, 1, \(x) {
  r <- rle(x)
  sum(r$lengths[r$values == 0] > 5)
})
#> [1] 0 1 0 0 0 0 1

Created on 2022-08-18 by the reprex package (v2.0.1)


Para obter as sequências de valores menores que ou iguais a 1 é um bocado mais complicado. Mas também pode ser resolvido com um ciclo apply, basta primeiro substituir os 1 por 0 e repetir o código acima.

apply(dados2, 1, \(x) {
  x[x <= 1] <- 0
  r <- rle(x)
  sum(r$lengths[r$values == 0] > 5)
})
#> [1] 0 1 0 0 1 0 1

Created on 2022-10-19 with reprex v2.0.2

Dados

x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados <- read.table(textConnection(x), header = TRUE, check.names = FALSE)

y<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   1   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados2 <- read.table(textConnection(y), header = TRUE, check.names = FALSE)
Reversão para edição 2
Fonte Link
Rui Barradas
  • 19mil
  • 2
  • 14
  • 28
r <x<-'1 apply(dados, 1, rle)
as.integer(sapply(r,2 \(x) sum(x$lengths[x$values ==3 0]) > 4   5))   6   7   8
#>1 [1]  10  5   0 1  0   25  12  0
2   0   0   0   0   0   0   1
as.integer(sapply(r,3 \(x) sum(x$lengths[x$values <=40 1]) >25  5))
#> [1]  0   1   0   32
4   0   10  6   3   0   0 1  2

Created on 2022-10-19 with reprex v2.0.2


Com a base dados2, que tem uma linha com sequências de zeros e uns de comprimentos maiores que 5.

s5 <- apply(dados2, 1,2 rle)  0   0   0   3   0   0
as.integer(sapply(s,6 \(x) sum(x$lengths[x$values ==10 0]) >3 5))  15  30  3   0   0
#>7 [1]  0 1  0   0   0   0 1  0   18'
asdados <- read.integertable(textConnection(x), header = TRUE, check.names = FALSE)

r <- apply(dados, 1, rle)
sapply(sr, \(x) sum(x$lengths[x$values <=== 1])0] > 5))
#> [1] 0 1 0 0 10 0 1

Created on 2022-1008-19 with18 by the reprex v2.0.2package (v2.0.1)

Dados em formato dput

x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12 dados 0<-
2   0   0   0   0   0   0   1structure(list(
3   40  25  5   0 `1` = 1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
:7   0   0   0   0   0,  
  0   18'
dados`2` <-= read.table(textConnectionc(x)10L, header0L, =40L, TRUE0L, check.names2L, =10L, FALSE0L)

y<-'1   2   3   4   5   6   7  , 8
1   10  5   0 `3` = 0c(5L, 0L, 25L, 2510L, 0L, 123L, 0L), 0
2   0   0   0   0`4` = c(0L, 00L, 5L, 6L, 00L, 15L, 0L), 1
3   40  25  5   0`5` = c(0L, 10L, 0L, 3L, 00L, 30L, 0L), 32
4   0   10  6   3`6` = c(25L, 00L, 1L, 0L, 03L, 3L, 0L), 2
5   2   0   0   0`7` = c(12L, 10L, 0L, 0L, 00L, 0L, 0L), 0
6   10  3   15 `8` 30= c(0L, 31L, 32L, 2L, 00L, 0L, 18L)), 0
7   0   0   0   0   0   0   18'
dados2 <- read.table(textConnection(y), headerclass = TRUE"data.frame", checkrow.names = FALSEc(NA, -7L))
r <- apply(dados, 1, rle)
as.integer(sapply(r, \(x) sum(x$lengths[x$values == 0]) > 5))
#> [1] 0 1 0 0 0 0 1
as.integer(sapply(r, \(x) sum(x$lengths[x$values <= 1]) > 5))
#> [1] 0 1 0 0 0 0 1

Created on 2022-10-19 with reprex v2.0.2


Com a base dados2, que tem uma linha com sequências de zeros e uns de comprimentos maiores que 5.

s <- apply(dados2, 1, rle)
as.integer(sapply(s, \(x) sum(x$lengths[x$values == 0]) > 5))
#> [1] 0 1 0 0 0 0 1
as.integer(sapply(s, \(x) sum(x$lengths[x$values <= 1]) > 5))
#> [1] 0 1 0 0 1 0 1

Created on 2022-10-19 with reprex v2.0.2

Dados

x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados <- read.table(textConnection(x), header = TRUE, check.names = FALSE)

y<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   1   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados2 <- read.table(textConnection(y), header = TRUE, check.names = FALSE)
x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados <- read.table(textConnection(x), header = TRUE, check.names = FALSE)

r <- apply(dados, 1, rle)
sapply(r, \(x) sum(x$lengths[x$values == 0] > 5))
#> [1] 0 1 0 0 0 0 1

Created on 2022-08-18 by the reprex package (v2.0.1)

Dados em formato dput

dados <-
  structure(list(
    `1` = 1:7,  
    `2` = c(10L, 0L, 40L, 0L, 2L, 10L, 0L), 
    `3` = c(5L, 0L, 25L, 10L, 0L, 3L, 0L), 
    `4` = c(0L, 0L, 5L, 6L, 0L, 15L, 0L), 
    `5` = c(0L, 0L, 0L, 3L, 0L, 30L, 0L), 
    `6` = c(25L, 0L, 1L, 0L, 3L, 3L, 0L), 
    `7` = c(12L, 0L, 0L, 0L, 0L, 0L, 0L), 
    `8` = c(0L, 1L, 32L, 2L, 0L, 0L, 18L)), 
    class = "data.frame", row.names = c(NA, -7L))
Código corrigido.
Fonte Link
Rui Barradas
  • 19mil
  • 2
  • 14
  • 28
x<-'1   2   3   4   5   6   7r <- apply(dados, 8
1   10  5   0   0, rle)
as.integer(sapply(r, \(x) 25sum(x$lengths[x$values == 120]) > 05))
2 #> [1] 0  1 0   0   0   0   0   1
3 as.integer(sapply(r, \(x) 40sum(x$lengths[x$values <= 251]) > 5 ))
#> [1] 0   1   0   32
4   0   10  6   3   0   0   21
5   2   0   0   0   3  

Created on 2022-10-19 with reprex v2.0.2


Com a base dados2, que tem uma linha com sequências de zeros e uns de comprimentos maiores que 5.

s 0<- apply(dados2, 1, 0rle)
6   10  3   15  30  3as.integer(sapply(s, \(x) sum(x$lengths[x$values 0== 0]) > 05))
7   0 #> [1] 0  1 0   0   0   0   18'1
dados <- read.table(textConnection(x), header = TRUE, checkas.names = FALSE)

r <- applyinteger(dados, 1, rle)
sapply(rs, \(x) sum(x$lengths[x$values ==<= 0]1]) > 5))
#> [1] 0 1 0 0 01 0 1

Created on 2022-0810-18 by the19 with reprex packagev2.0.2 (v2.0.1)

Dados em formato dput

dados <x<-'1   2   3   4   5   6   7   8
1  structure(list( 10  5   0   0   25  12  0
2   0 `1` = 0   0   0   0   0   1:7,
3   40  25  5   0   1   0   32
4   0 `2` = c(10L,10 0L, 40L,6 0L, 2L, 10L,3 0L),  0   0   2
5   2 `3` = c(5L,0 0L, 25L, 10L,0 0L, 3L, 0L),0   3   0   0
6   10 `4` =3 c(0L, 0L, 5L,15 6L, 0L,30 15L, 0L),3   0   0
7   0 `5` = c0   0   0   0   0   18'
dados <- read.table(0LtextConnection(x), 0L,header 0L,= 3LTRUE, 0L,check.names 30L,= 0LFALSE),

y<-'1   2   3   4   5   6   7   8
1   10 `6` =5 c(25L, 0L, 1L,0 0L, 3L, 3L,0 0L),  25  12  0
2   0 `7` = c(12L,0 0L, 0L, 0L,0 0L, 0L, 0L),0   0   0   1
3   40 `8` =25 c(0L, 1L,5 32L, 2L, 0L,0 0L, 18L)), 1   0   32
4   0 class = "data10  6   3   0   0   2
5   2   0   0   0   1   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados2 <- read.frame"table(textConnection(y), row.namesheader = c(NATRUE, -7L)check.names = FALSE)
x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados <- read.table(textConnection(x), header = TRUE, check.names = FALSE)

r <- apply(dados, 1, rle)
sapply(r, \(x) sum(x$lengths[x$values == 0] > 5))
#> [1] 0 1 0 0 0 0 1

Created on 2022-08-18 by the reprex package (v2.0.1)

Dados em formato dput

dados <-
  structure(list(
    `1` = 1:7, 
    `2` = c(10L, 0L, 40L, 0L, 2L, 10L, 0L), 
    `3` = c(5L, 0L, 25L, 10L, 0L, 3L, 0L), 
    `4` = c(0L, 0L, 5L, 6L, 0L, 15L, 0L), 
    `5` = c(0L, 0L, 0L, 3L, 0L, 30L, 0L), 
    `6` = c(25L, 0L, 1L, 0L, 3L, 3L, 0L), 
    `7` = c(12L, 0L, 0L, 0L, 0L, 0L, 0L), 
    `8` = c(0L, 1L, 32L, 2L, 0L, 0L, 18L)), 
    class = "data.frame", row.names = c(NA, -7L))
r <- apply(dados, 1, rle)
as.integer(sapply(r, \(x) sum(x$lengths[x$values == 0]) > 5))
#> [1] 0 1 0 0 0 0 1
as.integer(sapply(r, \(x) sum(x$lengths[x$values <= 1]) > 5))
#> [1] 0 1 0 0 0 0 1

Created on 2022-10-19 with reprex v2.0.2


Com a base dados2, que tem uma linha com sequências de zeros e uns de comprimentos maiores que 5.

s <- apply(dados2, 1, rle)
as.integer(sapply(s, \(x) sum(x$lengths[x$values == 0]) > 5))
#> [1] 0 1 0 0 0 0 1
as.integer(sapply(s, \(x) sum(x$lengths[x$values <= 1]) > 5))
#> [1] 0 1 0 0 1 0 1

Created on 2022-10-19 with reprex v2.0.2

Dados

x<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   3   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados <- read.table(textConnection(x), header = TRUE, check.names = FALSE)

y<-'1   2   3   4   5   6   7   8
1   10  5   0   0   25  12  0
2   0   0   0   0   0   0   1
3   40  25  5   0   1   0   32
4   0   10  6   3   0   0   2
5   2   0   0   0   1   0   0
6   10  3   15  30  3   0   0
7   0   0   0   0   0   0   18'
dados2 <- read.table(textConnection(y), header = TRUE, check.names = FALSE)
Acrescentados os dados em formato dput.
Fonte Link
Rui Barradas
  • 19mil
  • 2
  • 14
  • 28
Carregando
Fonte Link
Rui Barradas
  • 19mil
  • 2
  • 14
  • 28
Carregando