dfnums_to_numeric: Get the class for each column in a list

Description Usage Arguments Details Value Note Author(s) References See Also Examples

View source: R/BioGeoBEARS_generics_v1.R

Description

This function converts each column to class numeric where possible, and class character otherwise.

Usage

1
2
  dfnums_to_numeric(dtf, max_NAs = 0.5, printout = FALSE,
    roundval = NULL)

Arguments

dtf

Input data.frame.

max_NAs

Non-numeric cells will get converted to NA, up to the fraction of cells specified by max_NAs. Above this fraction, the column is converted to class character.

printout

Print the results to screen, if desired.

roundval

If not NULL, round will be run using this for the number of digits.

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 unlist_df4 for more, and cls.df to see the class of each column.

WARNING: IF A COLUMN IS A MIX OF NUMBERS AND NON-NUMBERS, THE NON-NUMBERS WILL BE CONVERTED TO NA IF THE COLUMN IS MAJORITY NUMBERS (on default; see max_NAs).

Value

dtf The output data.frame.

Note

Go BEARS!

Author(s)

Nicholas J. Matzke matzke@berkeley.edu

References

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

Matzke_2012_IBS

See Also

cls.df, unlist_df4

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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)

BioGeoBEARS documentation built on May 29, 2017, 8:36 p.m.