Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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 - Fatal编程技术网

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

R 具有成对距离矩阵输出的列名和行名,r,R,编辑: 我正在尝试收集这些值/列/行 **数字略有变化 我试图提取以下矩阵的成对结果 ID1_2001 ID2_2001 ID3_2001 ID1_2000 ID2_2000 ID2_2001 0.96747537 ID3_2001 0.96850817 0.67983338 ID1_2000 0.113

编辑:

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

**数字略有变化

我试图提取以下矩阵的成对结果

           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.1132489
0.7533675
0.8157735
的值

多亏了这个网站上的另一位用户,我知道了以下函数
proxy::dist(m[1:3],m[4:6],pairwise=TRUE,method=“cosine”)
,它给了我以下结果
0.1132489 0.7533675 0.8157735

但是,我也希望得到结果的列名和行名。因此,
0.1132489
将分配给
ID1\u 2000\u ID1\u 2001
0.7533675
分配给
ID2\u 2000\u ID2\u 2001
,最后
0.81577353
分配给
ID3\u 2000\u ID3\u 2001
。但是,我无法将此距离矩阵放入数据帧中,以访问/提取行名称和列名称

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

如何将
m[1:3]
替换为“组”,即取
2001
组,然后取
2000
组。因为我希望将其扩展到更多年/ID,并且我无法计算所有年/ID的行数
1:3
4:6

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

编辑:

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

x <- data.matrix(dist.matrix)
x <- as.data.frame(x)
编辑3:

我运行以下命令:

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


df <- data.frame(row   = rownames(dist.matrix), 
                 col   = colnames(dist.matrix), 
                 value = as.vector(dist.matrix))
编辑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

只需将行名和列名粘贴在数据框中,与数据本身一起即可。将矩阵“分解”为向量(并对名称进行向量循环)将解决其余问题:

# 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
#示例数据

mat我发现
rownames(dist.matrix)
colnames(dist.matrix)
只给出
dist
矩阵中的第一列/行名,并没有提供所有的列或行名。我的一个解决方案是
属性(dist.matrix)$Labels
。我无法让您的方法处理
距离矩阵
。我用一个更新的“data.frame”方法编辑了(在底部)我的原始消息…但是我不确定它是否能很好地推广到一个非常大的矩阵上。这有效吗
data.frame(rownames(dist.matrix)、colnames(dist.matrix)、as.vector(dist.matrix))
。我需要一些时间来理解从这里到哪里去(我把输出放在原始帖子的
EDIT2
下)。EDIT2不是你想要的吗?我不知道为什么
rownames(dist.matrix)
只返回
“ID1_2001”
但是当我按照你的方式去做/把它放进去时,我得到了完整的行名/列名。。。
> 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
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
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
# 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