更多主题>>
CSV - R:如何转换到一个特定的格式,并保存距离矩阵 -

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因此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") 

The distance matrix d is obtained as follows:距离矩阵d得到如下:

  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.通过键入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") 

I want to covert the distance matrix to a 3 columns with new headers and save it as a csv file as follows:我想隐蔽的距离到一个新的头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 
  • This is quite doable using base R functions.使用基地的R函数,这是一个相当可行的。 First we want all pairwise combinations of the rows to fill the columns c1 and c2 in the resulting object.首先,我们要行两两组合,以填补c1c2列在生成的目标。 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).在最后一列的distance来实现通过简单"dist" d d "dist"转换成数值向量(它已经是一个向量,但不同的类)。

    The first step is done using combn(rownames(x), 2) and the second step via as.numeric(d) :第一个步骤是通过使用combn(rownames(x), 2)和第二步骤通过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 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 

    To save as a CSV file, write.csv(m, file = "filename.csv") .要保存为一个CSV的文件, write.csv(m, file = "filename.csv")

  • you can do this by combining melt from reshape package, upper.tri etc.:你可以做到这一点的熔体重塑包,upper.tri等相结合:

     > library(reshape) > m <- as.matrix(d) > m aaa bbb ccc ddd aaa 0.0000000 1.0000000 0.6666667 0.5000000 bbb 1.0000000 0.0000000 0.3333333 0.6666667 ccc 0.6666667 0.3333333 0.0000000 0.3333333 ddd 0.5000000 0.6666667 0.3333333 0.0000000 > m2 <- melt(m)[melt(upper.tri(m))$value,] > names(m2) <- c("c1", "c2", "distance") > m2 c1 c2 distance 5 aaa bbb 1.0000000 9 aaa ccc 0.6666667 10 bbb ccc 0.3333333 13 aaa ddd 0.5000000 14 bbb ddd 0.6666667 15 ccc ddd 0.3333333