将距离矩阵转换并保存为特定格式
我通过以下步骤得到了一个距离矩阵:
x <- read.table(textConnection('
t0 t1 t2
aaa 0 1 0
bbb 1 0 1
ccc 1 1 1
ddd 1 1 0
' ), header=TRUE)
因此 x
是一个带有列和行标题的数据框
t0 t1 t2
aaa 0 1 0
bbb 1 0 1
ccc 1 1 1
ddd 1 1 0
require(vegan)
d <- vegdist(x, method="jaccard")
距离矩阵 d 的获取方式如下:
aaa bbb ccc
bbb 1.0000000
ccc 0.6666667 0.3333333
ddd 0.5000000 0.6666667 0.3333333
通过输入 str(d),我发现它不是普通表格也不是 csv 格式。
Class 'dist' atomic [1:6] 1 0.667 0.5 0.333 0.667 ...
..- attr(*, "Size")= int 4
..- attr(*, "Labels")= chr [1:4] "aaa" "bbb" "ccc" "ddd"
..- attr(*, "Diag")= logi FALSE
..- attr(*, "Upper")= logi FALSE
..- attr(*, "method")= chr "jaccard"
..- attr(*, "call")= language vegdist(x = a, method = "jaccard")
我想将距离矩阵转换为带有新标题的 3 列,并将其保存为 csv 文件,如下所示:
c1 c2 distance
aaa bbb 1.000
aaa ccc 0.6666667
aaa ddd 0.5
bbb ccc 0.3333333
bbb ddd 0.6666667
ccc ddd 0.3333333
I got a distance matrix with the following steps:
x <- read.table(textConnection('
t0 t1 t2
aaa 0 1 0
bbb 1 0 1
ccc 1 1 1
ddd 1 1 0
' ), header=TRUE)
As such x
is a data frame with column and row headers
t0 t1 t2
aaa 0 1 0
bbb 1 0 1
ccc 1 1 1
ddd 1 1 0
require(vegan)
d <- vegdist(x, method="jaccard")
The distance matrix d is obtained as follows:
aaa bbb ccc
bbb 1.0000000
ccc 0.6666667 0.3333333
ddd 0.5000000 0.6666667 0.3333333
By typing str(d), I found it is not a ordinary table nor csv format.
Class 'dist' atomic [1:6] 1 0.667 0.5 0.333 0.667 ...
..- attr(*, "Size")= int 4
..- attr(*, "Labels")= chr [1:4] "aaa" "bbb" "ccc" "ddd"
..- attr(*, "Diag")= logi FALSE
..- attr(*, "Upper")= logi FALSE
..- attr(*, "method")= chr "jaccard"
..- attr(*, "call")= language vegdist(x = a, method = "jaccard")
I want to covert the distance matrix to a 3 columns with new headers and save it as a csv file as follows:
c1 c2 distance
aaa bbb 1.000
aaa ccc 0.6666667
aaa ddd 0.5
bbb ccc 0.3333333
bbb ddd 0.6666667
ccc ddd 0.3333333
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用基本 R 函数这是完全可行的。首先,我们希望行的所有成对组合填充结果对象中的
c1
和c2
列。最后一列distance
是通过简单地将"dist"
对象d
转换为数值向量(它已经是一个向量,但具有不同的向量)来实现的。班级)。第一步使用
combn(rownames(x), 2)
完成,第二步通过as.numeric(d)
完成:给出:
保存为 CSV 文件,
write.csv(m, file = "filename.csv")
。This is quite doable using base R functions. First we want all pairwise combinations of the rows to fill the columns
c1
andc2
in the resulting object. The final columndistance
is achieved by simply converting the"dist"
objectd
into a numeric vector (it already is a vector but of a different class).The first step is done using
combn(rownames(x), 2)
and the second step viaas.numeric(d)
:Which gives:
To save as a CSV file,
write.csv(m, file = "filename.csv")
.你可以通过组合来自 reshape 包、upper.tri 等的melt来做到这一点:
you can do this by combining melt from reshape package, upper.tri etc.: