Input file is
Mydata <- 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=T
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