R:提取具有相同行和标题名称的值

时间:2016-10-21 18:51:23

标签: r matrix

我正在尝试从表中提取具有相同行标题和列标题的值。例如,我有一个这样的表:

    A   B   B   C   D   E   E
A   1   2   3   4   5   6   7
B   8   9   10  11  12  13  14  
C   15  16  17  18  19  20  21
D   22  23  24  25  26  27  28
E   29  30  31  32  33  34  35

我想提取值来得到这样的东西:

A   1
B   9
B   10
C   18
D   26
E   34
E   35

关于我如何做到这一点的任何想法?它必须适用于任意数量的行和列,并且可能有两个以上具有相同标题的列。

2 个答案:

答案 0 :(得分:4)

您可以跨行/列名称sapply来测试相等性,然后使用该索引来汇编data.frame:

i <- sapply(colnames(mat), `==`, rownames(mat))    # or i <- outer(rownames(mat), colnames(mat), `==`)

i
##          A     B     B     C     D     E     E
## [1,]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [2,] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
## [5,] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE

df <- data.frame(name = colnames(mat)[colSums(i) > 0],    # if unsure of dimensions, name = if(nrow(mat) > ncol(mat)){rownames(mat)[rowSums(i) > 0]}else{colnames(mat)[colSums(i) > 0]}
                 value = mat[i])

df
##   name value
## 1    A     1
## 2    B     9
## 3    B    10
## 4    C    18
## 5    D    26
## 6    E    34
## 7    E    35

数据

mat <- structure(c(1L, 8L, 15L, 22L, 29L, 2L, 9L, 16L, 23L, 30L, 3L, 
    10L, 17L, 24L, 31L, 4L, 11L, 18L, 25L, 32L, 5L, 12L, 19L, 26L, 
    33L, 6L, 13L, 20L, 27L, 34L, 7L, 14L, 21L, 28L, 35L), .Dim = c(5L, 
    7L), .Dimnames = list(c("A", "B", "C", "D", "E"), c("A", "B", 
    "B", "C", "D", "E", "E")))

答案 1 :(得分:0)

tab = structure(c(1L, 8L, 15L, 22L, 29L, 2L, 9L, 16L, 23L, 30L, 3L, 10L, 17L, 24L, 31L, 4L, 11L, 18L, 25L, 32L, 5L, 12L, 19L, 26L, 33L, 6L, 13L, 20L, 27L, 34L, 7L, 14L, 21L, 28L, 35L), .Dim = c(5L, 7L), .Dimnames = list(c("A", "B", "C", "D", "E"), c("A", "B", "B", "C", "D", "E", "E"))) 
cols = colnames(tab)[col(tab)]
rows = rownames(tab)[row(tab)]
ind = (cols == rows)
a = tab[ind]
names(a) = cols[ind]