metan (version 1.2.1)

utils_num_str: Utilities for handling with numbers and strings

Description

  • all_lower_case(): Translate all non-numeric strings of a data frame to lower case.

  • all_upper_case(): Translate all non-numeric strings of a data frame to upper case.

  • extract_number(): Extract the number(s) of a string.

  • extract_string(): Extract all strings, ignoring case.

  • remove_strings(): Remove all strings of a variable.

  • replace_number(): Replace numbers with a replacement.

  • replace_string(): Replace all strings with a replacement, ignoring case.

  • round_cols(): Round a selected column or a whole data frame to significant figures.

Usage

all_upper_case(.data)

all_lower_case(.data)

extract_number( .data, var, new_var = new_var, drop = FALSE, pull = FALSE, .before = NULL, .after = NULL )

extract_string( .data, var, new_var = new_var, drop = FALSE, pull = FALSE, .before = NULL, .after = NULL )

remove_strings(.data, ...)

replace_number( .data, var, new_var = new_var, pattern = NULL, replacement = "", drop = FALSE, pull = FALSE, .before = NULL, .after = NULL )

replace_string( .data, var, new_var = new_var, pattern = NULL, replacement = "", drop = FALSE, pull = FALSE, .before = NULL, .after = NULL )

round_cols(.data, ..., digits = 2)

Arguments

.data

A data frame

var

The variable to extract or replace numbers or strings.

new_var

The name of the new variable containing the numbers or strings extracted or replaced. Defaults to new_var.

drop

Logical argument. If TRUE keeps the new variable new_var and drops the existing ones. Defaults to FALSE.

pull

Logical argument. If TRUE, returns the last column (on the assumption that's the column you've created most recently), as a vector.

.before, .after

For replace_sting(), replace_number(), extract_string(), ,and extract_number() one-based column index or column name where to add the new columns.

...

The argument depends on the function used.

  • For round_cols() ... are the variables to round. If no variable is informed, all the numeric variables from data are used.

  • For remove_strings() ... are the variables to remove the strings. If no variable is informed, the strings of all non-numeric variables will be removed, keeping the numbers. If variables contains only strings, they will be replaced with NA.

pattern

A string to be matched. Regular Expression Syntax is also allowed.

replacement

A string for replacement.

digits

The number of significant figures.

Examples

Run this code
# NOT RUN {
library(metan)

################ Rounding numbers ###############
# All numeric columns
round_cols(data_ge2, digits = 1)

# Round specific columns
round_cols(data_ge2, EP, digits = 1)

########### Extract or replace numbers ##########
# Extract numbers
extract_number(data_ge, GEN)
extract_number(data_ge,
               var = GEN,
               drop = TRUE,
               new_var = g_number)

# Replace numbers

replace_number(data_ge, GEN)
replace_number(data_ge,
               var = GEN,
               pattern = "1",
               replacement = "_one",
               pull = TRUE)

########## Extract, replace or remove strings ##########
# Extract strings
extract_string(data_ge, GEN)
extract_string(data_ge,
               var = GEN,
               drop = TRUE,
               new_var = g_name)

# Replace strings
replace_string(data_ge, GEN)
replace_string(data_ge,
               var = GEN,
               new_var = GENOTYPE,
               pattern = "G",
               replacement = "GENOTYPE_")
# Remove strings
remove_strings(data_ge)
remove_strings(data_ge, ENV)
############# upper and lower cases ############
all_lower_case("GENOTYPE")
lc <- all_lower_case(data_ge)
lc
all_lower_case("GENOTYPE")

all_upper_case("Genotype")
all_upper_case(lc)
# }

Run the code above in your browser using DataLab