I would like to create a vector of names using the tree$edge rows of a phylogenetic tree. How can I construct this vector? Example:
tree$edge # object
[,1] [,2]
[1,] 82 83
[2,] 83 84
[3,] 84 85
[4,] 85 86
[5,] 86 87
[6,] 87 88
[7,] 88 89
[8,] 89 90
[9,] 90 91
[10,] 91 1
[11,] 91 92
[12,] 92 2
[13,] 92 93
[14,] 93 94
[15,] 94 3
[16,] 94 4
[17,] 93 95
[18,] 95 96
[19,] 96 5
[20,] 96 6
I would like to create a vector (that I call "vector_names) that each element is one of the rows of tree$edge. I can do this doing row by row:
vector_names = c(paste(tree$edge[1,1],tree$edge[1,2], sep = "_"),
paste(tree$edge[2,1],tree$edge[2,2], sep = "_"),
paste(tree$edge[3,1],tree$edge[3,2], sep = "_"),
and so on until last row ....)
The result will be:
vector_names = 82_83, 83_84, 84_85, 85_86, etc.
but is a long table with 161 rows and I would like to know if there is a faster way to produce the vector_names above.
Perhaps you can try fread
from data.table
like below
tree <- as.matrix(fread(text = paste0(paste0(vector_names, "\t")), sep = "_"))
or read.table
from base R
tree <- as.matrix(read.table(text = paste0(paste0(vector_names, "\t")), sep = "_"))
which gives
V1 V2
[1,] 82 83
[2,] 83 84
[3,] 84 85
[4,] 85 86
Update
If you want to get vector_names
from tree, you can try
> do.call(paste, c(data.frame(tree), sep = "-"))
[1] "82-83" "83-84" "84-85" "85-86"
Data
vector_names <- c("82_83", "83_84", "84_85", "85_86")
tree <- structure(c(82, 83, 84, 85, 83, 84, 85, 86), .Dim = c(4L, 2L))