本文介绍了将距离矩阵转换并保存为特定格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我得到一个包含以下步骤的距离矩阵: 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格式。 dist'atomic [1:6] 1 0.667 0.5 0.333 0.667 ... ..- attr(*,Size)= int 4 ..- attr chr [1:4]aaabbbcccddd ..- attr(*,Diag)= logi FALSE ..- attr )= logi FALSE ..- attr(*,method)= chrjaccard ..- 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 解决方案这是非常可行的使用基本R函数。首先,我们希望行的所有成对组合填充结果对象中的列 c1 和 c2 。最后一列 distance 是通过简单地转换dist object d 到一个数字向量(它已经是一个向量,但是不同的类)。 第一步使用 combn rownames(x),2),第二步通过 as.numeric(d): $ b b m names ; - c(c1,c2,distance) p> > m c1 c2 distance 1 aaa bbb 1.0000000 2 aaa ccc 0.6666667 3 aaa ddd 0.5000000 4 bbb ccc 0.3333333 5 bbb ddd 0.6666667 6 ccc ddd 0.3333333 要保存为CSV文件,请输入 csv(m,file =filename.csv)。 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 t2aaa 0 1 0bbb 1 0 1ccc 1 1 1ddd 1 1 0require(vegan)d <- vegdist(x, method="jaccard")The distance matrix d is obtained as follows: aaa bbb cccbbb 1.0000000 ccc 0.6666667 0.3333333 ddd 0.5000000 0.6666667 0.3333333By 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 distanceaaa bbb 1.000aaa ccc 0.6666667aaa ddd 0.5bbb ccc 0.3333333bbb ddd 0.6666667ccc ddd 0.3333333 解决方案 This is quite doable using base R functions. First we want all pairwise combinations of the rows to fill the columns c1 and c2 in the resulting object. The final column distance is achieved by simply converting the "dist" object d 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 via as.numeric(d):m <- data.frame(t(combn(rownames(x),2)), as.numeric(d))names(m) <- c("c1", "c2", "distance")Which gives:> m c1 c2 distance1 aaa bbb 1.00000002 aaa ccc 0.66666673 aaa ddd 0.50000004 bbb ccc 0.33333335 bbb ddd 0.66666676 ccc ddd 0.3333333To save as a CSV file, write.csv(m, file = "filename.csv"). 这篇关于将距离矩阵转换并保存为特定格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 08:00