如何检查矩阵的列是否遵循R中的预定顺序?

时间:2016-12-12 15:57:05

标签: r

矩阵看起来像......

      [,1]   [,2]   [,3]   [,4]   [,5]  
 [1,] "notB" "notB" "B"    "notB" "notB"
 [2,] "notB" "notB" "notB" "notB" "notB"
 [3,] "notB" "notB" "notB" "notB" "notB"
 [4,] "B"    "notB" "notB" "notB" "B"   
 [5,] "notB" "notB" "notB" "notB" "notB"
 [6,] "notB" "B"    "B"    "notB" "B"   
 [7,] "notB" "notB" "notB" "B"    "notB"
 [8,] "B"    "B"    "B"    "B"    "B"   
 [9,] "B"    "B"    "notB" "B"    "notB"

并且想法是计算(在数千列的设置中),有多少人保持" B"元素在一起,在任何位置,例如, B B B notB notB notB notB notB notBnotB notB B B B notB notB notB notB

在上面发布的矩阵中,只有列[,4]符合B s"以及#34;的标准。

以下是生成矩阵的代码:

b=c(rep("B", 3), rep("notB", 6))
n = 1000
d = replicate(n,sample(b, replace=F))

2 个答案:

答案 0 :(得分:3)

以下是在基础R中使用rle的一种方法。

myMat
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "B"  "B"  "B"  "B"  "B"  "B"  "B"  "B"  "B"  "B"  
[2,] "NB" "B"  "NB" "B"  "NB" "B"  "B"  "NB" "NB" "NB" 
[3,] "NB" "NB" "B"  "B"  "NB" "B"  "NB" "B"  "B"  "B"  
[4,] "NB" "NB" "NB" "B"  "NB" "NB" "NB" "NB" "B"  "NB" 
[5,] "NB" "NB" "B"  "B"  "NB" "B"  "NB" "NB" "NB" "B"  
[6,] "NB" "NB" "B"  "B"  "B"  "NB" "NB" "B"  "NB" "NB" 

使用此示例矩阵,列1,2,4和7将B组合在一起。所有其他列的Bs由NB分隔。

myRLEs <- apply(myMat, 2, rle)
which(sapply(myRLEs, function(x) sum(x$values == "B") == 1))
[1] 1 2 4 7

第一行计算沿列的B和NB的运行长度,并返回包含此信息的列表。第二行sum(x$values == "B") == 1检查只有一个“B”实例,sapply对myRLE的每个元素应用此检查。 which返回此检查所持的位置。

数据

set.seed(1234)
myMat <- matrix(sample(c("B", "NB"), 60, replace=TRUE), 6)

答案 1 :(得分:3)

另一种方法:

apply(mat,2, function(c) all(diff(which(c=="B")) == 1))

这里我们apply每个列的一个函数,用于检查alldiff对应的元素的索引之间"B"之间的1是否为"B" ## V1 V2 V3 V4 V5 ##FALSE FALSE FALSE TRUE FALSE }。当且仅当所有which都在一起时,这种情况才会成立。

使用您发布的数据,可以得到:

which(apply(mat,2, function(c) all(diff(which(c=="B")) == 1)))
## V4
##  4

然后我们可以使用apply来提取这个为真的列:

apply(mat, 2, function(c){w <- which(c == "B"); length(w) == diff(range(w)) + 1})

或者,正如@IaroslavDomin评论的那样,我们可以改为"B"函数

1

这具有优雅,我们无需检查diff(range(w))的相邻索引的所有差异是"B"。相反,我们只需要检查最后一个和第一个(即mat <- structure(c("notB", "notB", "notB", "B", "notB", "notB", "notB", "B", "B", "notB", "notB", "notB", "notB", "notB", "B", "notB", "B", "B", "B", "notB", "notB", "notB", "notB", "B", "notB", "B", "notB", "notB", "notB", "notB", "notB", "notB", "notB", "B", "B", "B", "notB", "notB", "notB", "B", "notB", "B", "notB", "B", "notB"), .Dim = c(9L, 5L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5"))) V1 V2 V3 V4 V5 [1,] "notB" "notB" "B" "notB" "notB" [2,] "notB" "notB" "notB" "notB" "notB" [3,] "notB" "notB" "notB" "notB" "notB" [4,] "B" "notB" "notB" "notB" "B" [5,] "notB" "notB" "notB" "notB" "notB" [6,] "notB" "B" "B" "notB" "B" [7,] "notB" "notB" "notB" "B" "notB" [8,] "B" "B" "B" "B" "B" [9,] "B" "B" "notB" "B" "notB" 加1)之间的差异是否与列中Dec 12 North Up 2 Down 3 South Up 4 Down 17 etc 的数量相匹配。

数据:

 { "_id" : ObjectId(), "direction" : String, "procedure" : String, "date" : String, .... , "format" : "procedure" }