Title: | 'dplyr' Functionality for Matched Tree and Data Objects |
---|---|
Description: | Matches phylogenetic trees and trait data, and allows simultaneous manipulation of the tree and data using 'dplyr'. |
Authors: | Josef Uyeda [cre] , Luke Harmon [aut] |
Maintainer: | Josef Uyeda <[email protected]> |
License: | GPL-2 | GPL-3 |
Version: | 0.1.11 |
Built: | 2024-11-05 05:04:40 UTC |
Source: | https://github.com/uyedaj/treeplyr |
Anole data for aRbor functions
data(anolis)
data(anolis)
An object of class list
of length 2.
Apply detectCharacterType over an entire matrix
detectAllCharacters(mat, repeatsAsDiscrete = TRUE, cutoff = 0.1)
detectAllCharacters(mat, repeatsAsDiscrete = TRUE, cutoff = 0.1)
mat |
A matrix of data |
repeatsAsDiscrete |
If TRUE, consider numeric variables that repeat values exactly as discrete; see cutoff |
cutoff |
Cutoff value for deciding if numeric data might actually be descrete: 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)
Function to detect whether a character is continuous or discrete
detectCharacterType(dat, repeatsAsDiscrete = TRUE, cutoff = 0.1)
detectCharacterType(dat, repeatsAsDiscrete = TRUE, cutoff = 0.1)
dat |
A vector of data |
repeatsAsDiscrete |
If TRUE, consider numeric variables that repeat values exactly as discrete; see cutoff |
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
This function can be used to select a subset of species (rows) from a treedata object;
see filter
.
## S3 method for class 'treedata' filter(.data, ...) ## S3 method for class 'grouped_treedata' filter(.data, ...)
## S3 method for class 'treedata' filter(.data, ...) ## S3 method for class 'grouped_treedata' filter(.data, ...)
.data |
An object of class |
... |
Additional arguments to filter by |
An object of class treedata
with the dataset filtered by the specified criteria.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat, name_column=1) tdfilter <- filter(td, island=="Cuba", SVL > 3.5)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat, name_column=1) tdfilter <- filter(td, island=="Cuba", SVL > 3.5)
Filter a matrix, returning either all continuous or all discrete characters
filterMatrix(mat, charType, returnType = "discrete")
filterMatrix(mat, charType, returnType = "discrete")
mat |
A matrix of data |
charType |
A vector of character types (perhaps from detectAllCharacters) |
returnType |
Either discrete or continuous |
Matrix with only discrete or continuous characters
data(anolis) aType<-detectAllCharacters(anolis$dat) filterMatrix(anolis$dat, aType, "discrete")
data(anolis) aType<-detectAllCharacters(anolis$dat) filterMatrix(anolis$dat, aType, "discrete")
This function can be used to check if a treedata object contains factors and, if desired, convert all columns automatically to factors.
forceFactor(tdObject, return.factor = TRUE)
forceFactor(tdObject, return.factor = TRUE)
tdObject |
A |
return.factor |
If TRUE, then a treedata object with all factors will be returned;
columns will be forced into factors using |
If return.factor, then an object of class "treedata
" with all columns as
factors.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdforcefactor <- forceFactor(td)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdforcefactor <- forceFactor(td)
Force names for rows, columns, or both
forceNames(dat, nameType = "row")
forceNames(dat, nameType = "row")
dat |
A vector of data |
nameType |
either:
|
data(anolis) forceNames(anolis$dat, "row")
data(anolis) forceNames(anolis$dat, "row")
This function can be used to check if a treedata object contains numeric columns and, if desired, drop all non-numeric columns.
forceNumeric(tdObject, return.numeric = TRUE)
forceNumeric(tdObject, return.numeric = TRUE)
tdObject |
A |
return.numeric |
If TRUE, then a treedata object with all numeric columns will be returned; non-numeric columns will be removed. |
If return.numeric, then an object of class "treedata
" with only numeric columns.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdnumeric <- forceNumeric(td)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdnumeric <- forceNumeric(td)
A function for returning a named vector from a data frame or matrix with row names
getVector(td, ...)
getVector(td, ...)
td |
A treedata object |
... |
The name of the column to select |
A named vector
treedata
This function can be used to group a treedata object by some factor.
## S3 method for class 'treedata' group_by(.data, ..., add = FALSE) ## S3 method for class 'grouped_treedata' ungroup(x, ...)
## S3 method for class 'treedata' group_by(.data, ..., add = FALSE) ## S3 method for class 'grouped_treedata' ungroup(x, ...)
.data |
An object of class |
... |
The name of the grouping factor. |
add |
By default, when add = FALSE, group_by will override existing groups. To instead add to the existing groups, use add = TRUE |
x |
An object of class |
Groups the data frame and phylogeny by one of the factors in the data table.
An object of class grouped_treedata
.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdGrouped <- group_by(td, ecomorph) summarize(tdGrouped, ntips = length(phy$tip.label), totalBL = sum(phy$edge.length), meanSVL = mean(SVL), sdSVL = sd(SVL))
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdGrouped <- group_by(td, ecomorph) summarize(tdGrouped, ntips = length(phy$tip.label), totalBL = sum(phy$edge.length), meanSVL = mean(SVL), sdSVL = sd(SVL))
Row and column name check
hasNames(dat, nameType = "row")
hasNames(dat, nameType = "row")
dat |
A vector of data |
nameType |
either:
|
data(anolis) hasNames(anolis$dat, "row")
data(anolis) hasNames(anolis$dat, "row")
treedata
This function generates an object of class treedata
that ensures that the ordering of tip labels
and data remain intact. The object can be manipulated using dplyr
functions.
make.treedata(tree, data, name_column = "detect")
make.treedata(tree, data, name_column = "detect")
tree |
An object of class 'phylo' |
data |
A data frame or matrix |
name_column |
An optional argument that specifies the column of |
An object of class "treedata
". The tree is pruned of tips not represented in the data,
and the data is filtered for taxa not in the tree. The data is returned as a data frame tble that is
compatible with dplyr
functions.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat)
treedata
This function can be used to add new variables to a treedata object; see mutate
.
## S3 method for class 'treedata' mutate(.data, ...) ## S3 method for class 'grouped_treedata' mutate(.data, ...)
## S3 method for class 'treedata' mutate(.data, ...) ## S3 method for class 'grouped_treedata' mutate(.data, ...)
.data |
An object of class |
... |
Arguments to mutate the treedata object |
An object of class treedata
with new data added.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdmutate <- mutate(td, lnSVL = log(SVL), badassery = awesomeness + hostility)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdmutate <- mutate(td, lnSVL = log(SVL), badassery = awesomeness + hostility)
This function paints clades on the phylogeny and adds a data column that specifies to which clade each species belongs
paint_clades( tdObject, nclades = 1, name = "clades", interactive = TRUE, type = "nodes", ids = NULL, plot = TRUE )
paint_clades( tdObject, nclades = 1, name = "clades", interactive = TRUE, type = "nodes", ids = NULL, plot = TRUE )
tdObject |
A |
nclades |
The number of clades that will be specified if used interactively |
name |
The name of the resulting data column |
interactive |
If |
type |
Either "nodes" or "branches" specifying if the ids provided specify the branch id (assuming a
post-ordered tree) or the node number. Ignored if |
ids |
A vector of node numbers of branch numbers that specify clades. Ignored if |
plot |
If |
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td <- reorder(td, "postorder") td.painted <- paint_clades(td, interactive=FALSE, type="nodes", ids=c(184, 160, 135, 122), plot=TRUE) td.painted <- group_by(td.painted, clades) summarise(td.painted, psig1 = phytools::phylosig(setNames(SVL, phy$tip.label), tree=phy), meanSVL = mean(SVL))
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td <- reorder(td, "postorder") td.painted <- paint_clades(td, interactive=FALSE, type="nodes", ids=c(184, 160, 135, 122), plot=TRUE) td.painted <- group_by(td.painted, clades) summarise(td.painted, psig1 = phytools::phylosig(setNames(SVL, phy$tip.label), tree=phy), meanSVL = mean(SVL))
treedata
objectReorders a treedata
object. Both the tips and the data are automatically reordered to match.
reorder(tdObject, ...) ## S3 method for class 'treedata' reorder(tdObject, order = "postorder", index.only = FALSE, ...)
reorder(tdObject, ...) ## S3 method for class 'treedata' reorder(tdObject, order = "postorder", index.only = FALSE, ...)
tdObject |
An object of class |
... |
Additional arguments to reorder.phylo |
order |
Method for reordering |
index.only |
Whether a index is returned rather than the reordered treedata object |
An object of class treedata
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td <- reorder(td, "postorder")
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td <- reorder(td, "postorder")
treedata
This function can be used to select a subset of variables (columns) from a treedata object;
see select
.
## S3 method for class 'treedata' select(.data, ...)
## S3 method for class 'treedata' select(.data, ...)
.data |
An object of class |
... |
Additional arguments to select columns |
An object of class treedata
with specified variables selected.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdselect <- select(td, SVL, awesomeness)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdselect <- select(td, SVL, awesomeness)
treedata
This function can be used to drop tips from tree and data; see slice
.
## S3 method for class 'treedata' slice(.data, ...)
## S3 method for class 'treedata' slice(.data, ...)
.data |
An object of class |
... |
Integer row values |
An object of class treedata
.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdslice <- slice(td, 1:5) tdslice
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) tdslice <- slice(td, 1:5) tdslice
treedata
This function can be used to summarize a treedata object.
## S3 method for class 'treedata' summarise(.data, ...) ## S3 method for class 'grouped_treedata' summarise(.data, ...)
## S3 method for class 'treedata' summarise(.data, ...) ## S3 method for class 'grouped_treedata' summarise(.data, ...)
.data |
An object of class |
... |
Additional expressions by which to summarize data in the |
Summarizing treedata
objects allows expressions using the objects phy
. The treedata
object can also be grouped, with summary statistics being applied to the pruned groups and phylogenies.
An object of class tbl_df
with the requested summary data.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) summarize(td, ntips = length(phy$tip.label), meanSVL = mean(SVL), sdSVL = sd(SVL)) tdGrouped <- group_by(td, ecomorph) summarize(tdGrouped, ntips = length(phy$tip.label), totalBL = sum(phy$edge.length), meanSVL = mean(SVL), sdSVL = sd(SVL))
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) summarize(td, ntips = length(phy$tip.label), meanSVL = mean(SVL), sdSVL = sd(SVL)) tdGrouped <- group_by(td, ecomorph) summarize(tdGrouped, ntips = length(phy$tip.label), totalBL = sum(phy$edge.length), meanSVL = mean(SVL), sdSVL = sd(SVL))
Apply a function over all treedata object columns and return a list of results, analogously to the normal apply function
tdapply(tdObject, MARGIN, FUN, ...)
tdapply(tdObject, MARGIN, FUN, ...)
tdObject |
A treedata object |
MARGIN |
the margin over which the data is applied (e.g. 1 = rows, 2 = columns) |
FUN |
A function to apply over the data frame |
... |
Additional parameters passed on to FUN |
Note that if the parameter phy
is specified in the additional parameters (i.e. '...'),
then it will be substituted with the treedata
object $phy
.
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td %>% forceNumeric(.) %>% tdapply(., 2, phytools::phylosig, tree=phy)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td %>% forceNumeric(.) %>% tdapply(., 2, phytools::phylosig, tree=phy)
treedata
objectRun a function on a treedata
object
treedply(tdObject, ...) ## S3 method for class 'treedata' treedply(tdObject, ...)
treedply(tdObject, ...) ## S3 method for class 'treedata' treedply(tdObject, ...)
tdObject |
A treedata object |
... |
A function call. |
This function allows arbitrary R functions that use trees and data to be run on
treedata
objects.
Function output
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) treedply(td, geiger::fitContinuous(phy, getVector(td, SVL), model="BM", ncores=1)) treedply(td, phytools::phylosig(phy, getVector(td, awesomeness), "lambda", test=TRUE)) treedply(td, phytools::phenogram(phy, getVector(td, SVL), ftype="off", spread.labels=FALSE))
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) treedply(td, geiger::fitContinuous(phy, getVector(td, SVL), model="BM", ncores=1)) treedply(td, phytools::phylosig(phy, getVector(td, awesomeness), "lambda", test=TRUE)) treedply(td, phytools::phenogram(phy, getVector(td, SVL), ftype="off", spread.labels=FALSE))
treedata
objectApplies a function to the phylogeny in a treedata
object. If the order of
tips are changed, or if tips are dropped, then the data are automatically reordered
to match the tree.
treeply(tdObject, ...) ## S3 method for class 'treedata' treeply(tdObject, FUN, ...)
treeply(tdObject, ...) ## S3 method for class 'treedata' treeply(tdObject, FUN, ...)
tdObject |
An object of class |
... |
Additional arguments |
FUN |
A function that operates on an object of class 'phylo' |
An object of class treedata
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td2 <- treeply(td, drop.tip, 1:50) par(mfrow=c(1,2)) plot(td$phy) plot(td2$phy)
data(anolis) td <- make.treedata(anolis$phy, anolis$dat) td2 <- treeply(td, drop.tip, 1:50) par(mfrow=c(1,2)) plot(td$phy) plot(td2$phy)
Matches phylogenetic trees and trait data, and allows simultaneous manipulation of the tree and data using 'dplyr'.
Josef Uyeda
These functions have been removed to reflect changes in dplyr.
group_by_.treedata
: This function is defunct, use group_by instead.
mutate_.treedata
, mutate_.grouped_treedata
: This function is defunct, use mutate instead.
slice_.treedata
: This function is defunct, use slice instead.
select_.treedata
: This function is defunct, use select instead.
filter_.treedata
,filter_.grouped_treedata
: This function is defunct, use filter instead.