Replace Spaces in Column Names in R (2 Examples)

 

This tutorial shows how to remove blanks in variable names in the R programming language.

The content of the page is structured as follows:

Let’s start right away:

 

Creation of Example Data

Have a look at the following example data:

data <- data.frame("x 1" = 1:5,    # Create example data frame
                   "x  2" = letters[1:5],
                   "x _ 3" = 5:1,
                   check.names = FALSE)
data                               # Print example data frame

 

table 1 data frame replace spaces column names r

 

Table 1 shows that our example data is constructed of five rows and three columns. The variable names contain multiple spaces – a typical phenomenon when data is imported from a file on your computer to R.

 

Example 1: Fix Spaces in Column Names of Data Frame Using gsub() Function

The R code below illustrates how to delete blanks in variable names and exchange them with a point (i.e. “.”) using the gsub function in R.

Have a look at the following R code:

data_new1 <- data                  # Duplicate data
colnames(data_new1) <- gsub(" ", ".", colnames(data_new1))
data_new1                          # Print updated data

 

table 2 data frame replace spaces column names r

 

As shown in Table 2, we have created a new data frame without spaced in the column names.

 

Example 2: Fix Spaces in Column Names of Data Frame Using make.names() Function

Example 2 explains how to use the make.names function to replace blanks in the names of columns.

Check out the following R syntax:

data_new2 <- data                  # Duplicate data
colnames(data_new2) <- make.names(colnames(data_new2), unique = TRUE)
data_new2                          # Print updated data

 

table 3 data frame replace spaces column names r

 

By executing the previous code we have created Table 3, i.e. another data frame without spaces in the variable names. Whether you want to use the gsub function or the make.names function is a matter of taste.

Please note, in the examples of this tutorial we have substituted the blanks by dots. However, we could also replace these blanks by other symbols or letters such as _ or -. Furthermore, we could remove the blanks from our column names entirely.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I’m explaining the R programming code of this tutorial in the video:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might have a look at the related articles of https://www.statisticsglobe.com/:

 

In this R tutorial you have learned how to get rid of blanks in column names. In case you have additional comments and/or questions, don’t hesitate to let me know in the comments section below. In addition, please subscribe to my email newsletter for regular updates on the newest articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top