Re-convert character columns in existing data frame
Source:R/reguess_coltypes.R
reguess_coltypes.RdConvenience wrapper around all_decimal_points_to_dot() and readr::type_convert().
Optionally converts numeric-like characters with a comma as decimal point (as used in e.g. Germany) to dots,
then performs a re-guessing of column types of all character columns.
Arguments
- df
A data.frame or tibble which character columns class shall be re-guessed.
- convert_comma_to_dot
Shall numeric-like characters with a comma as decimal point like '1,2' be converted to '1.2' before column type guessing?
- silent
Suppress column specification message from
readr::type_convert()column type guessing.- ...
Arguments passed on to
readr::type_convertcol_typesOne of
NULL, acols()specification, or a string. Seevignette("readr")for more details.If
NULL, column types will be imputed using all rows.naCharacter vector of strings to interpret as missing values. Set this option to
character()to indicate no missing values.trim_wsShould leading and trailing whitespace (ASCII spaces and tabs) be trimmed from each field before parsing it?
localeThe locale controls defaults that vary from place to place. The default locale is US-centric (like R), but you can use
locale()to create your own locale that controls things like the default time zone, encoding, decimal mark, big mark, and day/month names.guess_integerIf
TRUE, guess integer types for whole numbers, ifFALSEguess numeric type for all numbers.
Examples
df <- data.frame(a = c(1, 2, 3), b = c(TRUE, FALSE, NA), c = factor(c(1, 2, 1)))
char_df <- all_to_character(df)
reguess_coltypes(char_df)
#> a b c
#> 1 1 TRUE 1
#> 2 2 FALSE 2
#> 3 3 NA 1
# convert column b to numeric if convert_comma_to_dot is set.
df <- data.frame(a = c("a,b", "1,b", "c,0"), b = c("1,1", "1,2", "1,3"))
reguess_coltypes(df, convert_comma_to_dot = TRUE)
#> a b
#> 1 a,b 1.1
#> 2 1,b 1.2
#> 3 c,0 1.3