Distance matrix in R

拟墨画扇 提交于 2020-01-05 01:25:05

问题


I have to create a distance matrix using R. My data is in an Excel file which contains 300 rows and 10 columns. I have to create distance matrix based on the values of 9th column. For example

   s s s s s
s  1
s  2 2
s  3 3 4
s  4 4 7 3
s  5 5 8 2 8

How to create this type of matrix?


回答1:


Easiest option I know, is to save your Excel sheet containing the data as a CSV file. Make sure that only the first row and column of the sheet contain any sample or variable names.

Then read into R using:

dat <- read.csv("path/to/my/file.csv")

and then use dist() on the 9th column to compute the dissimilarity matrix

dij <- dist(dat[, 9])

If you want something other than the Euclidean distance, see the options in ?dist and if those don't suit, try the daisy() function in recommended package cluster, or vegdist() function in package vegan or the proxy package.




回答2:


If your numbers are in a vector called z, then dist(z) returns a distance matrix of euclidean (sqrt(dx^2+dy^2)) values. See help(dist) for more info.



来源:https://stackoverflow.com/questions/6923547/distance-matrix-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!