# Example 1: Fix numeric names
<- c(10, 20, 30)
numbers <- make.names(numbers)
valid_names print(valid_names)
[1] "X10" "X20" "X30"
Steven P. Sanderson II, MPH
March 11, 2024
Ever tried to use a number or special character as a name for a variable or column in R, only to be met with an error? R has specific rules for what constitutes a valid name, and the make.names
function is your knight in shining armor when it comes to wrangling these names into something R understands.
Think of make.names
as a name janitor. It takes a vector of characters (potential names) and ensures they comply with R’s naming conventions. These conventions say a valid name:
if
, else
, or for
)Using make.names
is straightforward. You simply provide it with a character vector containing your desired names, and it returns a new vector with valid names. Here’s the basic syntax:
By default, make.names
doesn’t guarantee unique names. If you have duplicates, it might just keep them. To ensure unique names, add the unique = TRUE
argument:
This will modify duplicate names slightly to make them distinct.
Let’s see make.names
in action with some examples:
# Example 1: Fix numeric names
numbers <- c(10, 20, 30)
valid_names <- make.names(numbers)
print(valid_names)
[1] "X10" "X20" "X30"
In this case, make.names
prepends an “X” to each number to make them valid names.
# Example 2: Handle special characters
special_chars <- c("data#1", "result$", "graph!")
clean_names <- make.names(special_chars)
print(clean_names)
[1] "data.1" "result." "graph."
Here, make.names
removes special characters and replaces them with periods (except for “$” which is removed).
R is a playground for exploration. Here are some challenges to try with make.names
:
make.names
to see how it handles them.make.names
on a data frame’s column names. What happens?unique = TRUE
argument. Can you think of situations where it might be necessary?Remember, make.names
is your friend when dealing with non-standard names in R. By understanding its purpose and using it effectively, you can keep your R code clean and error-free. Happy coding!