df_as_num: Convert convertible columns in a data.frame to numeric.

df_as_numR Documentation

Convert convertible columns in a data.frame to numeric.

Description

Attempts to convert all columns in a data.frame to numeric. If that fails, keeps the original.

Usage

df_as_num(
  df,
  stringsAsFactors = F,
  smart_factor_conversion = T,
  always_skip_factors = F,
  remove_commas = T,
  skip_factors = NULL
)

Arguments

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.

Examples

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

Deleetdk/kirkegaard documentation built on April 1, 2024, 2:23 a.m.