R Remove Blanks in Variable Names of Data Frame (Example Code)

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

Creation of Exemplifying Data

df <- data.frame("col A" = c("A", "X", "Y"),    # Exemplifying data frame in R
                 "col B" = letters[10:8],
                 "col C" = 10:12,
                 check.names = FALSE)
df                                              # Displaying example data frame in RStudio
#   col A col B col C
# 1     A     j    10
# 2     X     i    11
# 3     Y     h    12

Example: Remove Blanks from Variable Names

df_updated <- df                                # Replicating data frame
colnames(df_updated) <- gsub(" ",               # Changing column names
df_updated                                      # Displaying new data frame
#   col_A col_B col_C
# 1     A     j    10
# 2     X     i    11
# 3     Y     h    12

Related Articles & Further Resources

Have a look at the following R tutorials. They focus on topics such as matrices, merging, and extracting data:

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.
You need to agree with the terms to proceed