Learn R Programming

Coxmos (version 1.1.5)

transformIllegalChars: transformIllegalChars

Description

Transforms illegal or problematic characters in variable names into safe placeholder tokens to ensure compatibility with programming or data handling environments. Optionally, this function can also reverse these transformations if needed.

Usage

transformIllegalChars(cn, except = NULL, recover = FALSE)

Value

A character vector of transformed (or recovered) variable names, with illegal characters replaced or restored as specified. If the final result is purely numeric, it is prefixed with "var_" to ensure validity.

Arguments

cn

A character vector. Each element is a variable name to be sanitized or recovered.

except

A character vector of illegal characters or replacement tokens that should be excluded from the transformation or recovery process (default: NULL).

recover

Logical. If TRUE, reverts tokens back to their original characters instead of applying the transformation (default: FALSE).

Author

Pedro Salguero Garcia. Maintainer: pedsalga@upv.edu.es

Details

The function transformIllegalChars replaces characters that are typically considered illegal or problematic in variable names—such as spaces, mathematical operators, or punctuation marks—with descriptive placeholder tokens (e.g., .space., .comma.). This is particularly useful for variable names when exporting data, using variable names in formulas, or avoiding syntax errors.

The function supports two modes:

  • Standard mode (recover = FALSE): Replaces illegal characters with placeholder tokens.

  • Recovery mode (recover = TRUE): Replaces placeholder tokens back to their original illegal characters.

The argument except allows specific characters or tokens to be exempted from transformation or recovery. Additionally, after processing, the function checks whether all resulting values are numeric and, if so, prepends "var_" to preserve name validity.

This function uses an auxiliary function deleteIllegalChars() to sanitize additional issues before applying transformations.

Examples

Run this code
# Example 1: Transform illegal characters
original_names <- c("hsa-let-7a-2-3p", "hsa-let-7a-3p")
custom_formula_names <- transformIllegalChars(original_names)

# Example 2: Recover original characters from transformed names
recovered <- transformIllegalChars(custom_formula_names, recover = TRUE)

Run the code above in your browser using DataLab