【问题标题】:colnames and rownames with pairwise distance matrix outputs具有成对距离矩阵输出的 colnames 和 rownames
【发布时间】:2019-04-18 19:48:17
【问题描述】:

编辑:

我正在尝试收集这些值/列/行

** 数字略有变化。

我正在尝试提取以下矩阵的成对结果。

           ID1_2001   ID2_2001   ID3_2001   ID1_2000   ID2_2000
ID2_2001 0.96747537                                            
ID3_2001 0.96850817 0.67983338                                 
ID1_2000 0.11324889 0.97507292 0.97586446                      
ID2_2000 1.00000000 0.75336751 0.83321843 1.00000000           
ID3_2000 1.00000000 0.76556229 0.81577353 1.00000000 0.05728332

0.11324890.75336750.8157735的值。

感谢此站点上的另一个用户,我知道以下函数 proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine"),它只给了我以下结果 0.1132489 0.7533675 0.8157735

但是,我还想要结果来自的列名和行名。所以0.1132489 将分配给ID1_2000_ID1_20010.7533675 分配给ID2_2000_ID2_2001,最后0.81577353 分配给ID3_2000_ID3_2001。但是我不能将此距离矩阵放入数据框中以访问/提取 row_names 和 colnames。

最好只运行以下proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine") 并获得成对结果及其列名和行名(节省计算时间)。

如何将m[1:3] 替换为“组”,即先使用2001 组,然后再使用2000 组。因为我希望将其扩展到更多年份/ID,所以我无法计算所有年份/ID 的行 1:34:6

library(tidyr)
x <- m %>%
  data.frame() %>%
  tibble::rownames_to_column("rownames") %>%
  separate(rownames, c("id", "year"), "_")

其他:

dist.matrix = proxy::dist(m, pairwise = TRUE, method = "cosine") 
proxy::dist(m[1:3,], m[4:6,], pairwise=TRUE, method="cosine")

数据:

data <- structure(c(0.96747537487273, 0.968508167135111, 0.113248890901578, 
1, 1, 0.67983337671352, 0.97507292188601, 0.753367507803825, 
0.765562291938692, 0.975864460398726, 0.833218430412641, 0.815773525411265, 
1, 1, 0.0572833227621783), Size = 6L, Labels = c("ID1_2001", 
"ID2_2001", "ID3_2001", "ID1_2000", "ID2_2000", "ID3_2000"), class = "dist", Diag =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, 2, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 
2, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 4, 2, 0, 1, 1, 0, 
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 2, 0, 0, 0, 
0, 0, 0, 1, 1), .Dim = c(6L, 196L), .Dimnames = list(Docs = c("ID1_2001", 
"ID2_2001", "ID3_2001", "ID1_2000", "ID2_2000", "ID3_2000"), 
    Terms = c("-field", "(22-yard)", "(doubles).", "(either", 
    "(known", "(singles)", "(specifically", "20-metre", "able", 
    "across", "activity", "adjudicated", "aided", "although", 
    "american", "appears", "appears.", "around", "association", 
    "australian", "badminton", "bails", "bails,", "balanced", 
    "ball", "bat--ball", "bat,", "batting", "beach", "bowled", 
    "bowled,", "bowling", "called", "can", "canadian", "casual", 
    "catching", "centre", "certain", "codes", "common", "commonly", 
    "communicate", "comprising", "context", "cord", "countries", 
    "countries);", "court", "court.", "covered", "cricket", "degrees", 
    "degrees,", "different", "dislodges", "dismiss", "dismissal", 
    "dismissed,", "doubles", "each", "either", "eleven", "end,", 
    "ends", "family", "felt", "field", "fielding", "football", 
    "football);", "football.[1][2]", "football;", "football12", 
    "form", "formal", "forms", "gaelic", "gain", "game", "games", 
    "goal", "goal.", "gridiron", "ground.", "half", "hit", "hits", 
    "hollow", "include", "individually", "indoor", "information.", 
    "innings", "international", "involve", "involve,", "kicking", 
    "known", "landing", "larger", "league", "maneuver", "match", 
    "match's", "matches.", "may", "means", "net", "object", "often", 
    "one", "opponent", "opponent's", "opposing", "opposite", 
    "outdoor", "per", "pitch", "places", "play", "played", "player", 
    "players", "point,", "points", "popular", "prevent", "racket", 
    "racquet", "racquets", "record", "rectangular", "refer", 
    "referee", "regional", "return", "return.", "roles.", "rubber", 
    "rugby", "rules", "runs", "score", "scored", "scorers", "scores", 
    "shuttlecock", "side", "sides", "single", "singles", "soccer", 
    "specifically", "sport", "sports", "statistical", "strike", 
    "striking", "strung", "stumps", "stumps.", "swap", "team", 
    "teams", "ten", "tennis", "the", "these", "they", "third", 
    "three", "tries", "two", "umpire", "umpires,", "unable", 
    "understood", "union", "union);", "unqualified", "unqualified,", 
    "uses", "using", "valid", "variations", "varying", "way", 
    "when", "whichever", "wicket", "will", "will.", "within", 
    "word", "yard")))

编辑:

我发现这个解决方法可以放入数据框中。不确定它在大型矩阵上的效率如何

x <- data.matrix(dist.matrix)
x <- as.data.frame(x)

编辑2:

> data.frame(rownames(dist.matrix), colnames(dist.matrix), as.vector(dist.matrix))
   rownames.dist.matrix. colnames.dist.matrix. as.vector.dist.matrix.
1               ID1_2001              ID2_2001             0.97192896
2               ID1_2001              ID2_2001             0.97288923
3               ID1_2001              ID2_2001             0.01505221
4               ID1_2001              ID2_2001             1.00000000
5               ID1_2001              ID2_2001             1.00000000
6               ID1_2001              ID2_2001             0.69527190
7               ID1_2001              ID2_2001             0.97565046
8               ID1_2001              ID2_2001             0.75908178
9               ID1_2001              ID2_2001             0.77099402
10              ID1_2001              ID2_2001             0.97648342
11              ID1_2001              ID2_2001             0.77840308
12              ID1_2001              ID2_2001             0.76921180
13              ID1_2001              ID2_2001             1.00000000
14              ID1_2001              ID2_2001             1.00000000
15              ID1_2001              ID2_2001             0.05728332

编辑 3:

我运行以下命令;

dist.matrix = as.matrix(dist.matrix)


df <- data.frame(row   = rownames(dist.matrix), 
                 col   = colnames(dist.matrix), 
                 value = as.vector(dist.matrix))

这给了我以下输出:

     row      col      value
1  ID1_2001 ID1_2001 0.00000000
2  ID2_2001 ID2_2001 0.97192896
3  ID3_2001 ID3_2001 0.97288923
4  ID1_2000 ID1_2000 0.01505221
5  ID2_2000 ID2_2000 1.00000000
6  ID3_2000 ID3_2000 1.00000000
7  ID1_2001 ID1_2001 0.97192896
8  ID2_2001 ID2_2001 0.00000000
9  ID3_2001 ID3_2001 0.69527190
10 ID1_2000 ID1_2000 0.97565046
11 ID2_2000 ID2_2000 0.75908178
12 ID3_2000 ID3_2000 0.77099402
13 ID1_2001 ID1_2001 0.97288923
14 ID2_2001 ID2_2001 0.69527190
15 ID3_2001 ID3_2001 0.00000000
16 ID1_2000 ID1_2000 0.97648342
17 ID2_2000 ID2_2000 0.77840308
18 ID3_2000 ID3_2000 0.76921180
19 ID1_2001 ID1_2001 0.01505221
20 ID2_2001 ID2_2001 0.97565046
21 ID3_2001 ID3_2001 0.97648342
22 ID1_2000 ID1_2000 0.00000000
23 ID2_2000 ID2_2000 1.00000000
24 ID3_2000 ID3_2000 1.00000000
25 ID1_2001 ID1_2001 1.00000000
26 ID2_2001 ID2_2001 0.75908178
27 ID3_2001 ID3_2001 0.77840308
28 ID1_2000 ID1_2000 1.00000000
29 ID2_2000 ID2_2000 0.00000000
30 ID3_2000 ID3_2000 0.05728332
31 ID1_2001 ID1_2001 1.00000000
32 ID2_2001 ID2_2001 0.77099402
33 ID3_2001 ID3_2001 0.76921180
34 ID1_2000 ID1_2000 1.00000000
35 ID2_2000 ID2_2000 0.05728332
36 ID3_2000 ID3_2000 0.00000000

编辑 4:

x <- data.matrix(dist.matrix)
x <- as.data.frame(x)

library(tibble)
library(tidyr)
y <- x %>%
  rownames_to_column("row") %>%
  separate(row, c("id_row", "year_row"), "_")


z <- melt(y)
z

w <- z %>%
  separate(variable, c("id_col", "year_col"), "_")

w

这似乎给了

> head(w)
  id_row year_row id_col year_col      value
1    ID1     2001    ID1     2001 0.00000000
2    ID2     2001    ID1     2001 0.97192896
3    ID3     2001    ID1     2001 0.97288923
4    ID1     2000    ID1     2001 0.01505221
5    ID2     2000    ID1     2001 1.00000000
6    ID3     2000    ID1     2001 1.00000000

【问题讨论】:

    标签: r


    【解决方案1】:

    只需将行名和列名与数据本身放在数据框中即可。将矩阵“分解”为向量(以及名称的向量循环)将处理其余部分:

    # example data
    mat <- matrix(1:100, 10, 10)
    rownames(mat) <- paste0("row",1:10)
    colnames(mat) <- paste0("col",1:10)
    
    # what you want
    df <- data.frame(row   = rownames(mat), 
                     col   = colnames(mat), 
                     value = as.vector(mat) )
    
    # take a look at the result
    head(df)
    #   row  col value
    # row1 col1     1
    # row2 col2     2
    # row3 col3     3
    # row4 col4     4
    # row5 col5     5
    # row6 col6     6
    

    【讨论】:

    • 我发现rownames(dist.matrix)colnames(dist.matrix) 只是给了我dist 矩阵中的第一个列/行名称,并没有提供所有列或行名称。我的一种解决方案是attributes(dist.matrix)$Labels。我无法让您的方法在dist matrix 上工作。我使用更新的“data.frame”方法编辑(在底部)我的原始消息......但是我不确定它是否会很好地推广到一个非常大的矩阵。
    • 这行得通吗? data.frame(rownames(dist.matrix), colnames(dist.matrix), as.vector(dist.matrix))
    • 我觉得……有点。我需要一些时间来了解从这里去哪里(我将输出放在原帖中的EDIT2 下)。
    • EDIT2 不是你想要的怎么办?
    • 我不知道为什么 rownames(dist.matrix) 只返回 "ID1_2001" 但是当我按照你的方式关注/放置它时,我会得到完整的行名/列名......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2011-08-08
    • 1970-01-01
    相关资源
    最近更新 更多