如何从距离矩阵中提取组内和组间距离?在R中 [英] how to extract intragroup and intergroup distances from a distance matrix? in R

查看:245
本文介绍了如何从距离矩阵中提取组内和组间距离?在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据集:

values<-c(0.002,0.3,0.4,0.005,0.6,0.2,0.001,0.002,0.3,0.01)
codes<-c("A_1","A_2","A_3","B_1","B_2","B_3","B_4","C_1","C_2","C_3")
names(values)<-codes

在代码中,字母表示一个组,而数字表示每个组中的一个案例.因此,我分为三组,每组3到4个案例(实际数据集要大得多,但这是一个子集).

In the codes, the letter indicates a group and the number a case within each group. Therefore I have three groups and 3 to 4 cases in each group (the actual dataset is much larger but this is a subset).

然后我计算距离矩阵:

dist(values)->dist.m

现在,我想在具有两列的数据集中转换dist.m:一列包含所有组内部"的距离(A_1和A_2之间,B_2和B_4之间的距离,等等...),另一列包含组之间"的距离(A_1和B_1之间,C_1和B_4之间,等等...)

Now I would like to convert the dist.m in a dataset with two columns: one containing the distances "inside" all groups (distance between A_1 and A_2, between B_2 and B_4, etc...), and another one containing the distances "between" groups (between A_1 and B_1, between C_1 and B_4, etc...)

在R中有什么简单的方法吗?

Is there any easy way to do this in R?

任何帮助将不胜感激.

非常感谢您.

蒂娜.

推荐答案

它们可以称为矩阵,但实际上不是.但是,有一个as.matrix函数可以让您获得矩阵索引:

They may call them matrices but they are really not. There is however an as.matrix function that will let you get matrix indexing:

> as.matrix(dist.m)[grep("A", codes), grep("A", codes) ]
      A_1   A_2   A_3
A_1 0.000 0.298 0.398
A_2 0.298 0.000 0.100
A_3 0.398 0.100 0.000

因此,您可以使用紧凑的代码获得第一部分:

So you can get the first part with pretty compact code:

> sapply(LETTERS[1:3], function(let) as.matrix(dist.m)[grep(let, codes), grep(let, codes) ]
+ )
$A
      A_1   A_2   A_3
A_1 0.000 0.298 0.398
A_2 0.298 0.000 0.100
A_3 0.398 0.100 0.000

$B
      B_1   B_2   B_3   B_4
B_1 0.000 0.595 0.195 0.004
B_2 0.595 0.000 0.400 0.599
B_3 0.195 0.400 0.000 0.199
B_4 0.004 0.599 0.199 0.000

$C
      C_1   C_2   C_3
C_1 0.000 0.298 0.008
C_2 0.298 0.000 0.290
C_3 0.008 0.290 0.000

然后使用否定逻辑寻址来获取其余内容:

Then use negative logical addressing to get the rest:

> sapply(LETTERS[1:3], function(let) as.matrix(dist.m)[grepl(let, codes), !grepl(let, codes) ]
+ )
$A
      B_1   B_2   B_3   B_4   C_1   C_2   C_3
A_1 0.003 0.598 0.198 0.001 0.000 0.298 0.008
A_2 0.295 0.300 0.100 0.299 0.298 0.000 0.290
A_3 0.395 0.200 0.200 0.399 0.398 0.100 0.390

$B
      A_1   A_2   A_3   C_1   C_2   C_3
B_1 0.003 0.295 0.395 0.003 0.295 0.005
B_2 0.598 0.300 0.200 0.598 0.300 0.590
B_3 0.198 0.100 0.200 0.198 0.100 0.190
B_4 0.001 0.299 0.399 0.001 0.299 0.009

$C
      A_1   A_2   A_3   B_1   B_2   B_3   B_4
C_1 0.000 0.298 0.398 0.003 0.598 0.198 0.001
C_2 0.298 0.000 0.100 0.295 0.300 0.100 0.299
C_3 0.008 0.290 0.390 0.005 0.590 0.190 0.009

我看不到将其表示为两列数据结构的方法,但是您可以在pkg :: reshape2中使用melt来获得三列结构:

I don't see a way of representing this as a two column data structure but you can use melt in pkg::reshape2 to get a three column structure:

> melt( as.matrix(dist.m)[grep("A", codes), grep("A", codes) ] )
  Var1 Var2 value
1  A_1  A_1 0.000
2  A_2  A_1 0.298
3  A_3  A_1 0.398
4  A_1  A_2 0.298
5  A_2  A_2 0.000
6  A_3  A_2 0.100
7  A_1  A_3 0.398
8  A_2  A_3 0.100
9  A_3  A_3 0.000

这将为您提供一个较长的数据帧以进行显示,但是将melt放在函数调用中很容易.

That would give you a rather long dataframe for display but it would be easy enough to put melt inside the function call.

这篇关于如何从距离矩阵中提取组内和组间距离?在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆