R: Import CSV with Column Names that Contain Spaces


If you import a CSV file into R that contains column names with spaces, R will automatically replace the spaces with dots to make the column names have “valid” variable names.

If you would like to import the CSV file and keep the spaces in the column names, you must use the argument check.names=FALSE as follows:

df <- read.csv("my_data.csv", check.names=FALSE)

This will import the CSV file into R and keep the spaces in the column names.

The following example shows how to use this syntax in practice.

Example: Import CSV into R with Column Names that Contain Spaces

Suppose we have the following CSV file called basketball.csv:

Notice that there are four column names in the CSV file and two of them contain spaces in the name.

If we use the read.csv() function to import this CSV file, R will automatically replace the spaces with dots:

#import CSV file
df <- read.csv('basketball_data.csv')

#view data frame
df

  team points.scored assists.collected rebounds
1    A            22                10        5
2    B            15                 6        5
3    C            33                 9       12
4    D            20                14        3
5    E            11                 4        3

Notice that dots have replaced the spaces in the two column names with spaces.

If you’d like to import the CSV file and keep the spaces in the column names, you can use the check.names=FALSE argument as follows:

#import CSV file and keep spaces in column names
df <- read.csv('basketball_data.csv', check.names=FALSE)

#view data frame
df

  team points scored assists collected rebounds
1    A            22                10        5
2    B            15                 6        5
3    C            33                 9       12
4    D            20                14        3
5    E            11                 4        3

Notice that the spaces have been kept in the two column names with spaces.

However, you should note that if you attempt to perform any calculations using these column names with spaces that you must surround them in single back-quotes (`) or you’ll receive an error.

For example, if you attempt to calculate the sum of the values in the points scored column without using single back-quotes you will receive an error:

#attempt to calculate sum of points scored column
sum(df$points scored)

Error: unexpected symbol in "sum(df$points scored"

Instead, you must surround the column name in single back-quotes (`) as follows:

#calculate sum of points scored column
sum(df$`points scored`)

[1] 101

Notice that we don’t receive an error this time.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Read a CSV from a URL in R
How to Merge Multiple CSV Files in R
How to Read Specific Rows from CSV File into R
How to Export a Data Frame to a CSV File in R

Featured Posts

Leave a Reply

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