R cluster with Tanimoto/Jaccard

不问归期 提交于 2019-11-30 17:04:25

What is it you don't understand? ?vegdist tells us that it returns an object of class "dist" so you can just remove the dist(....) line and replace it with one calling vegdist(....). For example:

require(vegan)
d <- vegdist(Mydata[, -1], method = "jaccard")
fit <- hclust(d, method="ward") 
plot(fit)

You need to drop the first column (and should have done in the Euclidean version you showed in your Q) as this is not data that should be used to form the dissimilarity matrix.

That will generate a warning:

Warning message:
In vegdist(Mydata[, -1], method = "jaccard") :
  you have empty rows: their dissimilarities may be meaningless in method jaccard

because row 3 contains no information to form the jaccard distance between it and the other samples. You might want to consider if the jaccard is most appropriate in such cases.

The OP now wants the gene labels as row names. The easiest option is to tell R this when reading the data in, using the row.names argument to read.table():

mydata2 <- read.table(con <- textConnection("gene treatment1 treatment2 treatment3
aaa 1 0 1
bbb 1 1 1
ccc 0 0 0
eee 0 1 0
"), header = TRUE, row.names = 1)
close(con)

giving:

> mydata2
    treatment1 treatment2 treatment3
aaa          1          0          1
bbb          1          1          1
ccc          0          0          0
eee          0          1          0

Or if the data are already in R and it is a pain to reload and redo previous computations, just assign the gene column to the row names and remove the gene column (using the original mydata):

rownames(mydata) <- mydata$gene
mydata <- mydata[, -1]

giving:

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