0

说你在 R

m = matrix(0, 10, 5, dimnames = list(
                                    c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), 
                                    c(1, 2, 3, 4, 5)))
m[1,] = c(0,0,0,0,1)
m[2,] = c(0,0,0,1,1)
m[3,] = c(0,0,1,1,1)
m[4,] = c(0,0,1,1,0)
m[5,] = c(1,0,0,0,0)
m[6,] = c(1,1,1,0,0)
m[7,] = c(0,1,1,0,0)
m[8,] = c(0,1,1,0,0)
m[9,] = c(0,1,1,1,0)
m[10,] = c(1,1,1,0,1)

上面你看到矩阵 m。我有另一个矩阵 p 这样:

p = matrix(0, 1, 5, dimnames = list(
                                    c("A"), 
                                    c(1, 2, 3, 4, 5)))
p[1,] = c(0.2,0.03,0.2,0.01,1)

我有一个带有特定值(p 值)的矩阵,只有一列。我想从 m 创建一个新矩阵,但只有在 p 中该列的值小于 0.05 的列

所以基本上,我想在 R 中:

for (i in 1:length(p){
    if (p[1,i] < 0.05){
        #remember i in a list to extract later?
        mycols.append(i)
}}

myNewMat <- matrix[,mycols]

谢谢

4

1 回答 1

1

试试下面,看看这是否是你想要的。

myNewMat <- m[,p < 0.05]
于 2013-11-12T10:07:40.413 回答