具有成对距离矩阵输出的列名和行名

时间:2019-04-18 19:48:18

标签: r

编辑:

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

enter image description here

**数字略有变化。

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

           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_2001,将0.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]替换为“ groups”,即采用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 = FALSE, Upper = FALSE, method = "cosine", call = proxy::dist(x = m,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 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)

EDIT2:

> 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

1 个答案:

答案 0 :(得分: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