changing the case of the variable names

I have variable names in lower case with special characters on my file. In order to match to a different file, I need to make my variable names with upper case and substitute "" for all special characters. And I need to prefix the variable names with "A".

The following code fixes the variable names but the dataset UPPER_names only has the column names and is missing the rest of the data. How do I get a data frame with all of the data and the new variable names?

Create Data

mydata <- data.frame(
"variable.1" = c(1, 2, 3),
"variable.2" = c(3, 4, 5),
"variable.3" = c(6, 7, 8)
)

print(mydata)

names <- names(mydata)
print(names)

UPPER_names <- paste0("A_", toupper(gsub("(\.+)", "_", names, perl=TRUE)))
print(UPPER_names)

You can substitute the new names back in as shown below. Does that answer all of your questions?

mydata <- data.frame(
  "variable.1" = c(1, 2, 3),
  "variable.2" = c(3, 4, 5),
  "variable.3" = c(6, 7, 8)
)

print(mydata)
#>   variable.1 variable.2 variable.3
#> 1          1          3          6
#> 2          2          4          7
#> 3          3          5          8

names <- names(mydata)
print(names)
#> [1] "variable.1" "variable.2" "variable.3"

UPPER_names <- paste0("A_", toupper(gsub("(\\.+)", "_", names, perl=TRUE)))
print(UPPER_names)
#> [1] "A_VARIABLE_1" "A_VARIABLE_2" "A_VARIABLE_3"
names(mydata) <- UPPER_names
mydata
#>   A_VARIABLE_1 A_VARIABLE_2 A_VARIABLE_3
#> 1            1            3            6
#> 2            2            4            7
#> 3            3            5            8

Created on 2020-02-13 by the reprex package (v0.2.1)

Thank you! Sometimes I get stuck on the simple stuff.

If you want to avoid regex then the following code will be useful:

library(dplyr)

names(mydata) %>% 
janitor::make_clean_names(case = "all_caps") %>% 
paste0("A_",.)
3 Likes

That works also. I'm pretty new to R. Why would I want to avoid regrex? Thanks for the help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.