How to Use make.names Function in R

Spread the love

When dealing with datasets in R, you often encounter variables or column names that are not syntactically valid or standardized. The make.names function in R is a powerful utility that converts character vectors to syntactically valid names for variables. This function has a range of applications, from data cleaning to automating various data tasks. This comprehensive article aims to provide a deep dive into how to use make.names in R effectively.

Table of Contents

  1. Introduction to make.names
  2. Syntax and Parameters
  3. Basic Usage
  4. Use Cases
  5. Working with Data Frames
  6. Benefits of Using make.names
  7. Advanced Topics
  8. Caveats and Pitfalls
  9. Conclusion

1. Introduction to make.names

In R, variable names must meet certain syntactical rules to be considered valid. For instance, variable names must start with a letter or a dot and can only contain letters, numbers, or underscores. The make.names function helps you convert any given character string into a syntactically valid variable name.

2. Syntax and Parameters

The basic syntax for make.names is as follows:

make.names(names, unique = FALSE, allow_ = TRUE)
  • names: A character vector containing the names to be converted.
  • unique: Logical. If TRUE, the function ensures that names are unique.
  • allow_: Logical. If TRUE, underscores are allowed; otherwise, they are converted to dots.

3. Basic Usage

Making a Single Name

make.names("example name")

This will output:

Making Multiple Names

make.names(c("example name", "another example"))

This will output: c("", "another.example").

4. Use Cases

Converting Space to Dot

make.names("my variable")

Will return: my.variable.

Handling Numbers at the Beginning


Will return: X1var.

Handling Special Characters


Will return: var...

Handling Duplicates

make.names(c("var", "var"), unique = TRUE)

Will return: c("var", "var.1").

5. Working with Data Frames

Renaming Columns

df <- data.frame("first name" = c("Alice", "Bob"), "1Age" = c(25, 30))
names(df) <- make.names(names(df))

The column names will be changed to and X1Age.

Automating Renaming Across Multiple Data Frames

Suppose you have a list of data frames and you want to ensure that all column names across these data frames are syntactically valid:

list_of_dfs <- list(df1, df2, df3)
list_of_dfs <- lapply(list_of_dfs, function(x) {
    names(x) <- make.names(names(x))

6. Benefits of Using make.names

  1. Data Standardization: Ensures that variable names across different datasets are standardized.
  2. Data Integrity: Helps prevent bugs that could arise due to syntactically incorrect variable names.
  3. Automation: Useful in pipelines where new data can introduce variable names that are not syntactically valid.

7. Advanced Topics

Using make.names with Other Functions

You can use make.names in combination with other functions to further customize the naming convention.

toupper(make.names("example name"))

Will return: EXAMPLE.NAME.

Setting allow_ to FALSE

By setting allow_ to FALSE, you can convert underscores to dots:

make.names("example_name", allow_ = FALSE)

Will return:

8. Caveats and Pitfalls

  1. Loss of Information: Special characters are converted to dots, which might result in a loss of information.
  2. Ambiguity: Different original names might result in the same syntactically valid name.

9. Conclusion

The make.names function in R is an incredibly useful tool for creating syntactically valid names, especially when you are dealing with large and complex datasets. It offers flexibility in ensuring the uniqueness of names and allows you to control the use of underscores. Mastering make.names can be a significant step toward writing robust, error-free R code for data manipulation and analysis.

Posted in RTagged

Leave a Reply