Title: | Manipulation of Matched Phylogenies and Data using 'data.table' |
---|---|
Description: | An implementation that combines trait data and a phylogenetic tree (or trees) into a single object of class treedata.table. The resulting object can be easily manipulated to simultaneously change the trait- and tree-level sampling. Currently implemented functions allow users to use a 'data.table' syntax when performing operations on the trait dataset within the treedata.table object. |
Authors: | Josef Uyeda [aut] , Cristian Roman-Palacios [aut, cre] , April Wright [aut] , Luke Harmon [ctb], Hugo Gruson [rev], Kari Norman [rev] |
Maintainer: | Cristian Roman-Palacios <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-11-27 03:54:49 UTC |
Source: | https://github.com/ropensci/treedata.table |
treedata.table
This function can be used to subset rows, select and compute on columns data.table.
## S3 method for class 'treedata.table' x[...]
## S3 method for class 'treedata.table' x[...]
x |
An object of class |
... |
Arguments in the structure of |
A new object of class treedata.table
with $dat
and $phy
corresponding with the changes set to $dat
using
data.table's structure.
data(anolis) anolis2 <- anolis$phy anolis2$tip.label[1] <- "NAA" anolis1 <- anolis$phy anolis1$tip.label[1] <- "NAA" trees <- list(anolis1, anolis2) class(trees) <- "multiPhylo" treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" # A phylo object that fully matches the data td <- as.treedata.table(tree = anolis$phy, data = anolis$dat) td <- as.treedata.table(anolis$phy, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)] # A multiphylo object that fully matches the data td <- as.treedata.table(tree = treesFM, data = anolis$dat) td <- as.treedata.table(treesFM, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)] # A phylo object that partially matches the data td <- as.treedata.table(tree = anolis1, data = anolis$dat) td <- as.treedata.table(anolis1, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)] # A multiphylo object that partially matches the data td <- as.treedata.table(tree = trees, data = anolis$dat) td <- as.treedata.table(trees, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)]
data(anolis) anolis2 <- anolis$phy anolis2$tip.label[1] <- "NAA" anolis1 <- anolis$phy anolis1$tip.label[1] <- "NAA" trees <- list(anolis1, anolis2) class(trees) <- "multiPhylo" treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" # A phylo object that fully matches the data td <- as.treedata.table(tree = anolis$phy, data = anolis$dat) td <- as.treedata.table(anolis$phy, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)] # A multiphylo object that fully matches the data td <- as.treedata.table(tree = treesFM, data = anolis$dat) td <- as.treedata.table(treesFM, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)] # A phylo object that partially matches the data td <- as.treedata.table(tree = anolis1, data = anolis$dat) td <- as.treedata.table(anolis1, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)] # A multiphylo object that partially matches the data td <- as.treedata.table(tree = trees, data = anolis$dat) td <- as.treedata.table(trees, anolis$dat) td[, SVL] td[island == "Cuba" & ecomorph == "TG", .(ecomorph, island, SVL)] td[, utils::head(.SD, 1), by = .(ecomorph, island)]
treedata.table
This function extracts a named vector for any trait from an object of class
treedata.table
.
## S3 method for class 'treedata.table' x[[..., exact = TRUE]]
## S3 method for class 'treedata.table' x[[..., exact = TRUE]]
x |
An object of class |
... |
Column name in class |
exact |
whether exact search should be conducted |
A new object of class vector
with names set to labels corresponding
to tip labels in the provided treedata.table
object.
data(anolis) # With a phylo object td <- as.treedata.table(anolis$phy, anolis$dat) td[["SVL"]] # With a multiPhylo object treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" td <- as.treedata.table(treesFM, anolis$dat) td[["SVL"]]
data(anolis) # With a phylo object td <- as.treedata.table(anolis$phy, anolis$dat) td[["SVL"]] # With a multiPhylo object treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" td <- as.treedata.table(treesFM, anolis$dat) td[["SVL"]]
Anole data for treedata.table functions. Many of the traits (e.g. awesomeness, hostility) in this dataset retrieved from treeplyr are based on random numbers.
data(anolis)
data(anolis)
An object of class list
of length 2.
Luke Harmon
This function takes as input a tree of class phylo
or multiPhylo
and a
data.frame
and combines them into a treedata.table. If a multiPhylo
is
provided, all trees must have the same tip.labels. treedata.table
object is
sorted such that the rows in the data.table are matched to the tip.labels
of the phylogeny. Tip.labels on the tree must match a column of tip
names in the input data.frame. The output of this function will be a
treedata.table, which can be manipulated as a data.table.
as.treedata.table(tree, data, name_column = "detect")
as.treedata.table(tree, data, name_column = "detect")
tree |
A tree of class |
data |
A dataset in format |
name_column |
A character indicating the name of taxa in |
An object of type treedata.table
containing the tree and data.table
data(anolis) anolis2 <- anolis$phy anolis2$tip.label[1] <- "NAA" anolis1 <- anolis$phy anolis1$tip.label[1] <- "NAA" trees <- list(anolis1, anolis2) class(trees) <- "multiPhylo" treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" # A phylo object that fully matches the data td <- as.treedata.table(tree = anolis$phy, data = anolis$dat) # A multiphylo object that fully matches the data td <- as.treedata.table(tree = treesFM, data = anolis$dat) # A phylo object that partially matches the data td <- as.treedata.table(tree = anolis1, data = anolis$dat) # A multiphylo object that partially matches the data td <- as.treedata.table(tree = trees, data = anolis$dat)
data(anolis) anolis2 <- anolis$phy anolis2$tip.label[1] <- "NAA" anolis1 <- anolis$phy anolis1$tip.label[1] <- "NAA" trees <- list(anolis1, anolis2) class(trees) <- "multiPhylo" treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" # A phylo object that fully matches the data td <- as.treedata.table(tree = anolis$phy, data = anolis$dat) # A multiphylo object that fully matches the data td <- as.treedata.table(tree = treesFM, data = anolis$dat) # A phylo object that partially matches the data td <- as.treedata.table(tree = anolis1, data = anolis$dat) # A multiphylo object that partially matches the data td <- as.treedata.table(tree = trees, data = anolis$dat)
This function detects whether each column in a matrix is a continuous (e.g., with values 2.45, 9.35, and so on) or a discrete character (e.g., with values blue, red, yellow).
detectAllCharacters(mat, cutoff = 0.1)
detectAllCharacters(mat, cutoff = 0.1)
mat |
A matrix of data |
cutoff |
Cutoff value for deciding if numeric data might actually be discrete: if nlev is the number of levels and n the length of dat, then nlev / n should exceed cutoff, or the data will be classified as discrete |
Vector of either "discrete" or "continuous" for each variable in matrix
data(anolis) detectAllCharacters(anolis$dat)
data(anolis) detectAllCharacters(anolis$dat)
This function detects whether a given vector is a continuous (e.g., with values 2.45, 9.35, and so on) or a discrete (e.g., with values blue, red, yellow) character.
detectCharacterType(dat, cutoff = 0.1)
detectCharacterType(dat, cutoff = 0.1)
dat |
A vector of data |
cutoff |
Cutoff value for deciding if numeric data might actually be discrete: if nlev is the number of levels and n the length of dat, then nlev / n should exceed cutoff, or the data will be classified as discrete |
Either "discrete" or "continuous"
data(anolis) detectCharacterType(anolis$dat[, 1])
data(anolis) detectCharacterType(anolis$dat[, 1])
treedata.table
This function can be used to remove species from an object of class
treedata.table
. The resulting treedata.table
will include fully matching
$dat
and $phy
elements. The user should confirm the changes before they
are processed.
droptreedata.table(tdObject, taxa)
droptreedata.table(tdObject, taxa)
tdObject |
An object of class |
taxa |
A vector class |
An object of class treedata.table
with matching $dat
and $phy
elements based on whether taxa
were dropped or not.
data(anolis) # With a multiphylo object in the treedata.table object td <- as.treedata.table(anolis$phy, anolis$dat) droptreedata.table( tdObject = td, taxa = c("chamaeleonides", "eugenegrahami") ) # With a multiphylo object in the treedata.table object treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" td <- as.treedata.table(treesFM, anolis$dat) droptreedata.table( tdObject = td, taxa = c("chamaeleonides", "eugenegrahami") )
data(anolis) # With a multiphylo object in the treedata.table object td <- as.treedata.table(anolis$phy, anolis$dat) droptreedata.table( tdObject = td, taxa = c("chamaeleonides", "eugenegrahami") ) # With a multiphylo object in the treedata.table object treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" td <- as.treedata.table(treesFM, anolis$dat) droptreedata.table( tdObject = td, taxa = c("chamaeleonides", "eugenegrahami") )
Returning a named vector from a treedata.table object
extractVector(tdObject, ...)
extractVector(tdObject, ...)
tdObject |
A treedata.table object |
... |
The name of the column or columns to select. |
A named vector or a list of named vectors
data(anolis) td <- as.treedata.table(tree = anolis$phy, data = anolis$dat) # extracts the named vector for SVL from the td object extractVector(td, "SVL") # extracts the named vector for SVL and ecomorph from the td object extractVector(td, "SVL", "ecomorph")
data(anolis) td <- as.treedata.table(tree = anolis$phy, data = anolis$dat) # extracts the named vector for SVL from the td object extractVector(td, "SVL") # extracts the named vector for SVL and ecomorph from the td object extractVector(td, "SVL", "ecomorph")
This function filters a character matrix based on continuous (e.g., with values 2.45, 9.35, and so on) or discrete characters (e.g., with values blue, red, yellow).
filterMatrix(mat, returnType = "discrete")
filterMatrix(mat, returnType = "discrete")
mat |
A character matrix of class data.frame |
returnType |
Either discrete or continuous |
data.frame with only discrete (default) or continuous characters
data(anolis) filterMatrix(anolis$dat, "discrete")
data(anolis) filterMatrix(anolis$dat, "discrete")
This function creates column names (colnames
), row.names (row.names
),
or both in an unnamed data.frame
or matrix
.
forceNames(dat, nameType = "row")
forceNames(dat, nameType = "row")
dat |
A vector of data |
nameType |
either:
|
An object of type 'data.frame with labeled columns, rows, or both.
data(anolis) forceNames(anolis$dat, "row")
data(anolis) forceNames(anolis$dat, "row")
This function checks whether a given data.frame
or matrix
has
column names (colnames
), row.names (row.names
), or both.
hasNames(dat, nameType = "row")
hasNames(dat, nameType = "row")
dat |
A vector of data |
nameType |
either:
|
TRUE
or FALSE
indicating if the object has names (columns
,
rows
, or
both
)
data(anolis) hasNames(anolis$dat, "row")
data(anolis) hasNames(anolis$dat, "row")
Return the first part of an treedata.table object
## S3 method for class 'treedata.table' head(x, ...)
## S3 method for class 'treedata.table' head(x, ...)
x |
a treedata.table object |
... |
Additional arguments passed to head.data.table |
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) head(td)
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) head(td)
Print method treedata.table objects
## S3 method for class 'treedata.table' print(x, ...)
## S3 method for class 'treedata.table' print(x, ...)
x |
an object of class "treedata.table" |
... |
additional arguments passed to "head.treedata.table" |
Function uses prints the tree and the first lines of the data.table object.
Returns the character matrix or phylogeny from a treedata.table object
pulltreedata.table(tdObject, type = c("dat", "phy"))
pulltreedata.table(tdObject, type = c("dat", "phy"))
tdObject |
A treedata.table object |
type |
Whether the output of the function is a tree ('type="phylo"') or a data.table ('type="dat"') |
A data.table
or phylo
object from the original treedata.table
object
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) pulltreedata.table(td, type = "phy") pulltreedata.table(td, type = "dat")
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) pulltreedata.table(td, type = "phy") pulltreedata.table(td, type = "dat")
Summarizing treedata.table objects
## S3 method for class 'treedata.table' summary(object, ...)
## S3 method for class 'treedata.table' summary(object, ...)
object |
an object of class "treedata.table" |
... |
additional arguments passed to "head.treedata.table" |
Function tries to be smart about summarizing the data and detecting continuous vs. discrete data, as well as whether any data have missing data. Also returns the taxa that are dropped from either the original tree or the original data.
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) summary(td)
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) summary(td)
Return the last part of an treedata.table object
## S3 method for class 'treedata.table' tail(x, ...)
## S3 method for class 'treedata.table' tail(x, ...)
x |
a treedata.table object |
... |
Additional arguments passed to head.data.table |
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) tail(td)
data(anolis) td <- as.treedata.table(anolis$phy, anolis$dat) tail(td)
treedata.table
objectRun a function on a treedata.table
object
tdt(tdObject, ...)
tdt(tdObject, ...)
tdObject |
A treedata.table object |
... |
A function call. |
This function allows R functions that use trees and data to be run
ontreedata.table
objects.
Function output for a single tree (phylo) or a list of function outputs (one per each tree in the MultiPhylo object)
data(anolis) # A treedata.table object with a phylo $phy td <- as.treedata.table(anolis$phy, anolis$dat) tdt(td, geiger::fitContinuous(phy, extractVector(td, "SVL"), model = "BM", ncores = 1 )) # A treedata.table object with a multiPhylo $phy treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" td <- as.treedata.table(treesFM, anolis$dat) tdt(td, geiger::fitContinuous(phy, extractVector(td, "SVL"), model = "BM", ncores = 1 ))
data(anolis) # A treedata.table object with a phylo $phy td <- as.treedata.table(anolis$phy, anolis$dat) tdt(td, geiger::fitContinuous(phy, extractVector(td, "SVL"), model = "BM", ncores = 1 )) # A treedata.table object with a multiPhylo $phy treesFM <- list(anolis$phy, anolis$phy) class(treesFM) <- "multiPhylo" td <- as.treedata.table(treesFM, anolis$dat) tdt(td, geiger::fitContinuous(phy, extractVector(td, "SVL"), model = "BM", ncores = 1 ))