简体   繁体   English

如何在 R 中查找具有特定数量的连续 0 和 1 的矩阵中的行

[英]How to find rows in matrix with specific number of consecutive 0s and 1s in R

I have this matrix:我有这个矩阵:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
 [1,]    1    0    0    0    0    0    0    0
 [2,]    0    1    1    0    0    0    0    0
 [3,]    0    0    1    1    0    0    0    0
 [4,]    0    0    0    1    1    0    0    0 
 [5,]    0    0    0    0    1    1    0    0   
 [6,]    0    0    0    0    0    1    1    0   
 [7,]    0    0    0    0    0    0    1    1         

What I want to do is to filter this matrix by rows where the minimum number of consecutive 0s and 1s is n.我想要做的是按行过滤此矩阵,其中连续 0 和 1 的最小数量为 n。

For example, if n = 2 , the expected output should be this:例如,如果n = 2 ,预期的 output 应该是这样的:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
 [1,]    0    0    1    1    0    0    0    0
 [2,]    0    0    0    1    1    0    0    0 
 [3,]    0    0    0    0    1    1    0    0     
 [4,]    0    0    0    0    0    0    1    1   

As you can see the rows 1, 2 and 6 of the initial matrix had a minimum of only one consecutive 1, one consecutive 0 and one consecutive 0, respectively.如您所见,初始矩阵的第 1、2 和 6 行分别最少只有一个连续的 1、一个连续的 0 和一个连续的 0。

If n = 3 , the expected output should be an empty matrix since no row has a minimum of three consecutive 0s and three consecutive 1s simultaneously.如果n = 3 ,则预期的 output 应该是一个空矩阵,因为没有行同时具有最少三个连续的 0 和三个连续的 1。

Is there a way to create a function that takes as input a matrix and the value n and gives the expected output in R?有没有办法创建一个 function 作为输入矩阵和值 n 并在 R 中给出预期的 output?

This is the matrix of the example:这是示例的矩阵:

matrix(c(1, 0, 0, 0, 0, 0, 0, 0,
         0, 1, 1, 0, 0, 0, 0, 0,
         0, 0, 1, 1, 0, 0, 0, 0,
         0, 0, 0, 1, 1, 0, 0, 0, 
         0, 0, 0, 0, 1, 1, 0, 0,   
         0, 0, 0, 0, 0, 1, 1, 0,   
         0, 0, 0, 0, 0, 0, 1, 1), byrow = T, ncol = 8)

We can use rle row-wise and select the rows where the minimum consecutive length between numbers is at least n .我们可以使用rle row-wise 和 select 数字之间的最小连续长度至少为n的行。

n <- 2
mat[apply(mat, 1, function(x) all(rle(x)$lengths >= n)), ]

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,]    0    0    1    1    0    0    0    0
#[2,]    0    0    0    1    1    0    0    0
#[3,]    0    0    0    0    1    1    0    0
#[4,]    0    0    0    0    0    0    1    1

data数据

mat <- structure(c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = 7:8)

We can use rleid from data.table to create the condition for subsetting the rows我们可以使用rleid中的data.table来创建子集行的条件

library(data.table)
n <- 2
mat[apply(mat, 1, function(x) all(table(rleid(x))>=2)),]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,]    0    0    1    1    0    0    0    0
#[2,]    0    0    0    1    1    0    0    0
#[3,]    0    0    0    0    1    1    0    0
#[4,]    0    0    0    0    0    0    1    1

data数据

mat <- structure(c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = 7:8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM