fixVarNames: Fix variable names e.g. YP1, YP2 ... YP10 to YP01, YP02 ......

View source: R/fixVarNames.R

fixVarNamesR Documentation

Fix variable names e.g. YP1, YP2 ... YP10 to YP01, YP02 ... YP10

Description

Fix variable names e.g. YP1, YP2 ... YP10 to YP01, YP02 ... YP10

Usage

fixVarNames(nameTxt, width = 2, newText = "")

Arguments

nameTxt

character vector of variable names

width

number, restricted to 2 or 3

newText

character, to replace the existing character part of the names

Value

vector of the zero padded names, perhaps with new text element

Background

This is just a utility function that comes in useful if you want to change a vector of variable names that have the format xxxx# where "xxxx" is a character component and "#" is a number component. The function is vectorized, i.e. you can give it a vector of names or a single name. It can be used within the tidyverse dplyr pipeline with "rename_with()" which will apply the function to

History/development log

Started 17.x.24

Author(s)

Chris Evans

See Also

Other utility functions: convertClipboardAuthorNames(), getAttenuatedR(), getCorrectedR(), whichSetOfN()

Examples

### these show the function acting on single values (unusual use case but ...!)
fixVarNames("YP1")
fixVarNames("YP10")
fixVarNames("YP1", 3) # if you want that to become YP010, i.e. three digits
fixVarNames("YP10", 3) # same
fixVarNames("YP10", 3, "YPCORE_") # replaces "YP" with "YPCORE_"

### same but with vector (two element vector is hardly more sensible use case!)
fixVarNames(c("YP1", "YP2"))
fixVarNames(c("YP10", "YP11"))
fixVarNames(c("YP1", "YP2"), 3)
fixVarNames(c("YP10", "YP11"), 3)
fixVarNames(c("YP10", "YP11"), 3, "YPCORE_")


### create a dataset
### library(tidyverse) # to get the tidyverse functionality including tribble()
library(tidyr)
library(tibble)
library(dplyr)
tribble(~ID, ~I1, ~I2, ~I3, ~I4, ~I5, ~I6, ~I7, ~I8, ~I9, ~I10,
         "P1", 4, 0, 2, 3, 1, 1, 0, 2, 3, 2,
         "P2", 2, 3, 4, 2, 1, 2, 4, 3, 1, 2,
         "P3", 4, 1, 1, 2, 3, 1, 0, 4, 0, 3) -> tmpTib

### simple example, the "-ID" says "don't do this to the ID variable"
tmpTib %>%
  rename_with(fixVarNames, -ID)

### this next uses column indices to select the variables to recode
tmpTib %>%
  rename_with(fixVarNames, 2:10)

### this next uses explicit selection
tmpTib %>%
  rename_with(fixVarNames, .cols = I1 : I10)

### this illustrates passing arguments to fixVarNames() as trailing arguments
tmpTib %>%
  rename_with(fixVarNames, .cols = I1 : I10, width = 3, newText = "YP")

### new dataset so ID coding doesn't start with "I" so I can illustrate
### selecting with "starts_with()"
tribble(~P_ID, ~I1, ~I2, ~I3, ~I4, ~I5, ~I6, ~I7, ~I8, ~I9, ~I10,
        "P1", 4, 0, 2, 3, 1, 1, 0, 2, 3, 2,
        "P2", 2, 3, 4, 2, 1, 2, 4, 3, 1, 2,
        "P3", 4, 1, 1, 2, 3, 1, 0, 4, 0, 3) -> tmpTib

### select using starts_with()
tmpTib %>%
rename_with(fixVarNames, starts_with("I"))

### as before, put arguments after selection of the variables/columns
tmpTib %>%
rename_with(fixVarNames, starts_with("I"), width = 3, newText = "YP")



cpsyctc/CECPfuns documentation built on Nov. 16, 2024, 10:43 a.m.