如何识别矩阵中每行不是“NA”的列?

时间:2022-08-06 04:38:44

I have a matrix with 12 rows and 77 columns, but to simply lets use:

我有一个12行和77列的​​矩阵,但只是让我们使用:

p <- matrix(NA,5,7)  
p[1,2]<-0.3  
p[1,3]<-0.5  
p[2,4]<-0.9  
p[2,7]<-0.4  
p[4,5]<-0.6 

I want to know which columns are not "NA" per row, so what I would like to get would be something like:

我想知道每行哪些列不是“NA”,所以我想得到的是:

[1] 2,3  
[2] 4  
[3] 0  
[4] 5  
[5] 0 

but if I do > which(p[]!="NA") I get [1] 6 11 17 24 32

但如果我做>哪个(p []!=“NA”)我得到[1] 6 11 17 24 32

I tried using a loop:

我尝试使用循环:

aux <- matrix(NA,5,7)  
for(i in 1:5) {  
    aux[i,]<-which(p[i,]!="NA")  
}

but I just get an error: number of items to replace is not a multiple of replacement length

但我只是得到一个错误:要替换的项目数量不是替换长度的倍数

Is there a way of doing this? Thanks in advance

有办法做到这一点吗?提前致谢

1 个解决方案

#1


17  

Try:

尝试:

which( !is.na(p), arr.ind=TRUE)

Which I think is just as informative and probably more useful than the output you specified, But if you really wanted the list version, then this could be used:

我认为这只是提供信息,可能比你指定的输出更有用,但如果你真的想要列表版本,那么可以使用:

> apply(p, 1, function(x) which(!is.na(x)) )
[[1]]
[1] 2 3

[[2]]
[1] 4 7

[[3]]
integer(0)

[[4]]
[1] 5

[[5]]
integer(0)

Or even with smushing together with paste:

或者甚至与糊状物一起涂抹:

lapply(apply(p, 1, function(x) which(!is.na(x)) ) , paste, collapse=", ")

The output from which function the suggested method delivers the row and column of non-zero (TRUE) locations of logical tests:

建议方法的函数输出提供逻辑测试的非零(TRUE)位置的行和列:

> which( !is.na(p), arr.ind=TRUE)
     row col
[1,]   1   2
[2,]   1   3
[3,]   2   4
[4,]   4   5
[5,]   2   7

Without the arr.ind parameter set to non-default TRUE, you only get the "vector location" determined using the column major ordering the R has as its convention. R-matrices are just "folded vectors".

如果没有将arr.ind参数设置为非默认值TRUE,则只能使用R具有的列主要顺序来确定“向量位置”。 R矩阵只是“折叠向量”。

> which( !is.na(p) )
[1]  6 11 17 24 32

#1


17  

Try:

尝试:

which( !is.na(p), arr.ind=TRUE)

Which I think is just as informative and probably more useful than the output you specified, But if you really wanted the list version, then this could be used:

我认为这只是提供信息,可能比你指定的输出更有用,但如果你真的想要列表版本,那么可以使用:

> apply(p, 1, function(x) which(!is.na(x)) )
[[1]]
[1] 2 3

[[2]]
[1] 4 7

[[3]]
integer(0)

[[4]]
[1] 5

[[5]]
integer(0)

Or even with smushing together with paste:

或者甚至与糊状物一起涂抹:

lapply(apply(p, 1, function(x) which(!is.na(x)) ) , paste, collapse=", ")

The output from which function the suggested method delivers the row and column of non-zero (TRUE) locations of logical tests:

建议方法的函数输出提供逻辑测试的非零(TRUE)位置的行和列:

> which( !is.na(p), arr.ind=TRUE)
     row col
[1,]   1   2
[2,]   1   3
[3,]   2   4
[4,]   4   5
[5,]   2   7

Without the arr.ind parameter set to non-default TRUE, you only get the "vector location" determined using the column major ordering the R has as its convention. R-matrices are just "folded vectors".

如果没有将arr.ind参数设置为非默认值TRUE,则只能使用R具有的列主要顺序来确定“向量位置”。 R矩阵只是“折叠向量”。

> which( !is.na(p) )
[1]  6 11 17 24 32