Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 列出矩阵中某些值的位置_R_List_Matrix - Fatal编程技术网

R 列出矩阵中某些值的位置

R 列出矩阵中某些值的位置,r,list,matrix,R,List,Matrix,我有一个矩阵,里面有1和0。例如: M1 = matrix(c(1, 0, 1, 0, 1, 0), nrow=2, ncol=3, byrow = TRUE) row.names(M1) <- c(100, 101) colnames(M1) <- c("A", "B", "C") M1 A B C 100 1 0 1 101 0 1 0 在本例中,我正在寻找的解决方案是: 100.A, 1 100.C, 1 101.B, 1 试试这个: paste0(row.nam

我有一个矩阵,里面有
1
0
。例如:

M1 = matrix(c(1, 0, 1, 0, 1, 0), nrow=2, ncol=3, byrow = TRUE)
row.names(M1) <- c(100, 101)
colnames(M1) <- c("A", "B", "C")
M1

    A B C
100 1 0 1
101 0 1 0
在本例中,我正在寻找的解决方案是:

100.A, 1
100.C, 1
101.B, 1
试试这个:

paste0(row.names(M1==1), ".", colnames(M1==1))
x <- which(M1==1, arr.ind = TRUE)
data.frame(COL=paste0(rownames(M1)[x[,1]], ".", colnames(M1)[x[,2]], ", ", 1))

#       COL
#1 100.A, 1
#2 101.B, 1
#3 100.C, 1
或者,如果要添加1:

data.frame(result = paste0(row.names(M1==1), ".", colnames(M1==1), ", 1"))
编辑:超过1次时更正

result <- list()
for (i in 1:nrow(M1)){
  result[[i]] <- data.frame(result = paste0(rep(row.names(M1)[i], length(colnames(M1)[M1[i,]==1])), ".", colnames(M1)[M1[i,]==1], ", 1"))
}
result <- do.call(rbind, result)
result <- unique(result)
result试试这个:

paste0(row.names(M1==1), ".", colnames(M1==1))
x <- which(M1==1, arr.ind = TRUE)
data.frame(COL=paste0(rownames(M1)[x[,1]], ".", colnames(M1)[x[,2]], ", ", 1))

#       COL
#1 100.A, 1
#2 101.B, 1
#3 100.C, 1
或者,如果要添加1:

data.frame(result = paste0(row.names(M1==1), ".", colnames(M1==1), ", 1"))
编辑:超过1次时更正

result <- list()
for (i in 1:nrow(M1)){
  result[[i]] <- data.frame(result = paste0(rep(row.names(M1)[i], length(colnames(M1)[M1[i,]==1])), ".", colnames(M1)[M1[i,]==1], ", 1"))
}
result <- do.call(rbind, result)
result <- unique(result)
result试试这个:

paste0(row.names(M1==1), ".", colnames(M1==1))
x <- which(M1==1, arr.ind = TRUE)
data.frame(COL=paste0(rownames(M1)[x[,1]], ".", colnames(M1)[x[,2]], ", ", 1))

#       COL
#1 100.A, 1
#2 101.B, 1
#3 100.C, 1
x试试这个:

paste0(row.names(M1==1), ".", colnames(M1==1))
x <- which(M1==1, arr.ind = TRUE)
data.frame(COL=paste0(rownames(M1)[x[,1]], ".", colnames(M1)[x[,2]], ", ", 1))

#       COL
#1 100.A, 1
#2 101.B, 1
#3 100.C, 1

x感谢您的输入!但我认为如果矩阵有另一个结构,它就不起作用了。例如,如果一列中有多个值等于1,则感谢您的输入!但我认为如果矩阵有另一个结构,它就不起作用了。例如,如果一列中有多个值等于1