knitr::opts_chunk$set(echo = TRUE)

Additional functions for manipulating data

treedata.table includes additional functions that allow the identification of discrete and continuous characters in a given dataset. We first load the dataset:

library(ape)
library(treedata.table)

# Load example data
data(anolis)
#Create treedata.table object with as.treedata.table
td <- as.treedata.table(tree = anolis$phy, data = anolis$dat)

The detectCharacterType() function can be used to examine whether SVL is discrete or continuous:

detectCharacterType(anolis$dat$SVL)

We can further examine the type of characters we have in our dataset by using the detectAllCharacters() function:

detectAllCharacters(anolis$dat)

Summarizing this information in a table, we get:

cbind.data.frame(character=colnames(anolis$dat),type=detectAllCharacters(anolis$dat))

Finally, we can use the filterMatrix() function to subset our dataset to only a certain type of characters. For instance, let's extract all discrete characters in the Anolis dataset:

filterMatrix(anolis$dat, "discrete")

Two additional functions in treedata.table are designed to examine and modify column and row names in any dataset. For instance, we can ask if the Anolis dataset has column names:

hasNames(anolis$dat, "col")

It does have column names. Let's just remove the column names and check if hasNames() can detect this change. Here's our new dataset:

data=anolis$dat
colnames(data)<-NULL
head(data,2)

Let's run hasNames() on our new dataset:

hasNames(data, "col")

Now, we can create new column names using the forceNames() function:

data <- forceNames(data, "col")

The new dataset, with column names (n1...), looks like this:

head(data,2)

And we can finally ask whether our new dataset actually have column names by running the hasNames() function again:

hasNames(data, "col")

We can apply the same procedure for columns (col), rows (row) or both (rowcol).



ropensci/treedata.table documentation built on Sept. 12, 2021, 6:23 p.m.