df_as_num | R Documentation |
Attempts to convert all columns in a data.frame to numeric. If that fails, keeps the original.
df_as_num(
df,
stringsAsFactors = F,
smart_factor_conversion = T,
always_skip_factors = F,
remove_commas = T,
skip_factors = NULL
)
df |
A data.frame. |
stringsAsFactors |
Whether to convert strings to factors. Default is F. |
smart_factor_conversion |
Whether to automatically detect numeric factors. Default=T. This is done by checking whether the levels can be converted to numeric values. If they can, the factor is converted. |
always_skip_factors |
Whether to always skip non-ordered factors. Default=F. |
remove_commas |
Whether to remove commas from the cells first. If present, they will make the conversion to numeric fail. Defaults to T. |
skip_factors |
Depreciated. |
iris_chr = as.data.frame(lapply(iris, as.character)) #convert iris to strings
str(iris_chr)
str(df_as_num(iris_chr)) #convert back
all(iris == df_as_num(iris_chr)) #confirm identity
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.