Entering edit mode
9.0 years ago
Lila M
★
1.3k
I'm trying to create a minimum spanning tree with R. My problem is that I have my distances stored in a txt as follow:
PID1 PID2 distance
1 4 0.002
2 5 0.004
3 6 0.003
Does there exist a way in which I can use my txt as matrix? I tried to transform it but I couldn't. Thank you everyone in advance
My code:
dat=read.table("distances.txt", dec='.', header=T)
Matrix<- as.matrix(dat)
mstree <-mst(Matrix)
Error en `[<-`(`*tmp*`, j, index.i, value = 139127) :
This is not a bioinformatics question but an R programming one. You may have better luck getting an answer on StackOverflow.
First issue is that your data file is not in matrix form. as.matrix() is not going to do any rearranging, it just converts internal data types. Second, mstree() (from the spdep package) doesn't take a matrix as argument. Read the docs.
Finally, for graph-related work in R, I suggest using the igraph package.
I guess Lila M is using
mst()
from {ape} package, and it may take a matrix as argument.