Suppose you want to import CSV file and keep the spaces in the column names, you can use the check.names=FALSE argument.
The following method shows how you can do it with syntax.
Method 1: Import CSV file with Column Names Contains Spaces
df <- read.csv("data.csv",check.names=FALSE)
The following example shows how to import CSV with column names that contain spaces in R.
Import CSV file with Column Names Contains Spaces
Let’s first import csv file in R without using check.names argument:
# Import CSV file
df <- read.csv("Machine_data.csv")
# Print dataframe
print(df)
Output:
Date Machine.type Status Pressure Temperature
1 21/10/2023 Mach_1 TRUE 11 45.60
2 22/10/2023 Mach_2 TRUE 12 47.20
3 23/10/2023 Mach_3 FALSE 10 46.10
4 24/10/2023 Mach_4 TRUE 11 46.85
5 1/11/2023 Mach_A FALSE 89 10.10
6 2/11/2023 Mach_B TRUE 85 12.80
7 3/11/2023 Mach_C FALSE 87 13.74
8 4/11/2023 Mach_D FALSE 90 9.55
As we can see the space between column name Machine type replace by ..
# Import CSV file
df <- read.csv("Machine_data.csv", check.names=FALSE)
# Print dataframe
print(df)
Output:
Date Machine type Status Machine Pressure Machine Temperature
1 21/10/2023 Mach_1 TRUE 11 45.60
2 22/10/2023 Mach_2 TRUE 12 47.20
3 23/10/2023 Mach_3 FALSE 10 46.10
4 24/10/2023 Mach_4 TRUE 11 46.85
5 1/11/2023 Mach_A FALSE 89 10.10
6 2/11/2023 Mach_B TRUE 85 12.80
7 3/11/2023 Mach_C FALSE 87 13.74
8 4/11/2023 Mach_D FALSE 90 9.55
The output shows data from Machine_data.csv where space between column names is keep.
By following this syntax you can keep spaces between columns while importing CSV file.