这是我第一次使用R。
我有一个表,有3列和12090行(156个细菌)。前两列是细菌的名称,最后一列是指示有机体之间的相关性的数字(基于一种基因组相似性)。示例为(虚构的数字):
bacteria1 bacteria2 0.25846
bacteria1 bacteria3 0.35986
bacteria2 bacteria1 0.57896
bacteria2 bacteria3 0.54596
bacteria3 bacteria1 0.23659
bacteria3 bacteria2 0.36528
我希望能够将这些加入到某种系统发育树中。我看到'nj‘需要一个距离矩阵来做这件事。如何将其转换为距离矩阵或可用格式?(这些数字已经是距离,所以不应该做任何数学运算)我尝试过as.dist()、as.matrix()和reshape(),但是作为一个新手,我可能做错了所有的事情。(重塑可能就是我需要的。)
或者,如果有人知道如何通过其他方式把它们做成一棵树,那就太好了。
谢谢你的帮助。
发布于 2012-10-11 21:30:55
使用库reshape2
(它与base R中的重塑函数不同,我认为它有很多用处
# Load the library (after installing it, of course)
library(reshape2)
# Load up your data - for future reference, it's always helpful to post your data
# with a question. I used dput(x) to generate this structure below:
x <- structure(list(V1 = structure(c(1L, 1L, 2L, 2L, 3L, 3L),
.Label = c("bacteria1", "bacteria2", "bacteria3"),
class = "factor"), V2 = structure(c(2L, 3L, 1L, 3L, 1L, 2L),
.Label = c("bacteria1", "bacteria2", "bacteria3"), class = "factor"),
V3 = c(0.25846, 0.35986, 0.57896, 0.54596, 0.23659, 0.36528)),
.Names = c("V1", "V2", "V3"), class = "data.frame",
row.names = c(NA, -6L))
# Recast it - acast returns a matrix with V1 as the records, V2 as the columns,
# and V3 as the values
distmat <- acast(x, V1 ~ V2, value.var = "V3")
发布于 2012-10-12 00:20:13
听起来你有距离矩阵的上三角部分或下三角部分,但没有维度。(尽管您确定您有156行吗?如果有18种细菌,则应该有choose(18,2)
= 153个条目,而不是156个条目。)
假设您的表中确实有153行,您可以这样填充矩阵:
m <- matrix(nrow=18, ncol=18)
m[row(m) < col(m)] <- x # if it's the upper triangular portion
或
m[row(m) > col(m)] <- x # if it's the lower triangular portion
然后diag(m) <- 0
表示对角线。
https://stackoverflow.com/questions/12848635
复制相似问题