Learn R Programming

BioGeoBEARS (version 0.2.1)

cls.df: Get the class for each column in a list

Description

This function returns the class of each column in a data.frame.

Usage

cls.df(dtf, printout = FALSE)

Arguments

dtf
Input data.frame.
printout
Print the results to screen, if desired.

Value

dtf_classes A data.frame showing the column, column name, and column class.

Details

R does lots of weird and unpredictable things when you build up tables/matrices/data.frames by e.g. cbind and rbind on vectors of results. The major problems are (1) columns get made into class list; (2) numeric columns are converted to class factor; (3) numeric columns are converted to class character; (4) you have a matrix when you think you have a data.frame.

All of this could be taken care of by detailed understanding and tracking of when R recasts values in vectors, matrices, and data frames...but this is a huge pain, it is easier to just have a function that jams everything back to a data.frame with no lists, no factors, and with columns being numeric where possible. See dfnums_to_numeric and unlist_df4 for these options.

References

http://phylo.wikidot.com/matzke-2013-international-biogeography-society-poster

Matzke_2012_IBS

See Also

dfnums_to_numeric, unlist_df4

Examples

Run this code
x = matrix(c(1,2,3,4,5,6), nrow=3, ncol=2)
cls.df(x)
dfnums_to_numeric(adf(x))
unlist_df4(x)

x = matrix(c(1,2,3,4,5,"A"), nrow=3, ncol=2)
cls.df(x)
dfnums_to_numeric(adf(x))
unlist_df4(x)

x = adf(matrix(c(1,2,3,4,5,"A"), nrow=3, ncol=2))
names(x) = c("A","B")
cls.df(x)
dfnums_to_numeric(adf(x))
unlist_df4(x)

Run the code above in your browser using DataLab