Learn R Programming

umx (version 4.20.0)

umx_rename: umx_rename

Description

Returns a dataframe with variables renamed as desired.

Usage

umx_rename(
  data,
  from = NULL,
  to = NULL,
  regex = NULL,
  test = FALSE,
  old = "deprecated_from",
  replace = "deprecated_to"
)

Value

  • dataframe with columns renamed.

Arguments

data

The dataframe in which to rename variables

from

List of existing names that will be found and replaced by the contents of replace. (optional: Defaults to NULL).

to

If used alone, a named collection of c(oldName = "newName") pairs. OR, if "from" is a list of existing names, the list of new names) OR, if "regex" is a regular expression, the replace string)

regex

Regular expression with matches will be replaced using replace as the replace string. (Optional: Defaults to NULL).

test

Whether to report a "dry run", not changing anything. (Default = FALSE).

old

deprecated: use from

replace

deprecated: use to

Details

Unlike similar functions in other packages, it checks that the variables exist, and that the new names do not.

Importantly, it also supports regular expressions. This allows you to find and replace text based on patterns and replacements. so to change "replacement" to "in place", grep=re(place)ment, replace= in \\1.

note:To use replace list, you must say c(old = "new"), not c(old -> "new")

See Also

namez to filter (and replace) names, Also umx_check_names to check for existence of names in a dataframe.

Other Data Functions: noNAs(), prolific_anonymize(), prolific_check_ID(), prolific_read_demog(), umxFactor(), umxHetCor(), umx_as_numeric(), umx_cont_2_quantiles(), umx_lower2full(), umx_make_MR_data(), umx_make_TwinData(), umx_make_fake_data(), umx_make_raw_from_cov(), umx_merge_randomized_columns(), umx_polychoric(), umx_polypairwise(), umx_polytriowise(), umx_read_lower(), umx_reorder(), umx_score_scale(), umx_select_valid(), umx_stack(), umx_strings2numeric(), umx

Examples

Run this code
tmp = mtcars

tmp = umx_rename(tmp, to = c(cyl = "cylinder"))
# let's check cyl has been changed to cylinder...
namez(tmp, "c")

# Alternate style: from->to, first with a test-run
# Dry run
tmp = umx_rename(tmp, from = "disp", to = "displacement", test= TRUE)
# Actually do it
tmp = umx_rename(tmp, from = c("disp"), to = c("displacement"))
umx_check_names("displacement", data = tmp, die = TRUE)
namez(tmp, "disp")

# This will warn that "disp" does not exist (anymore)
new = c("auto", "displacement", "rear_axle_ratio")
tmp = umx_rename(tmp, from = c("am", "disp", "drat"), to = new)
namez(tmp, "a") # still updated am to auto (and rear_axle_ratio)

# Test using regex (in this case to revert "displacement" to "disp")
tmp = umx_rename(tmp, regex = "lacement", to = "", test= TRUE) 
tmp = umx_rename(tmp, regex = "lacement", to = "") # revert to disp
umx_names(tmp, "^d") # all names beginning with a d

# dev: check deprecated format handled...
tmp = umx_rename(tmp, old = c("am", "disp", "drat"), replace = new)

Run the code above in your browser using DataLab