Create DataFrame with Spaces in Column Names in R

16 May, 2021
In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language.
The data.frame() method in R can be used to create a data frame with individual rows and columns in R. This method contains an attribute check.names, which is by default set to TRUE while making this call. The role of this attribute is to validate that the names assigned to the variable are syntactically valid. It also ensures that the names are not duplicated. In case there is any violation in the names assigned, these are adjusted implicitly by make.names attribute. 
make.names attribute assigned allowed string names to the columns of the data frame, where in any invalid characters are transformed to a ‘.’ It takes as input a character vector and returns the output as the same length character vector where each cell value is coerced to a legal name. 
A syntactically valid name consists of letters, numbers, and the dot or underline characters and starts with a letter or the dot not followed by a number.
Syntax:
make.names(names, unique=FALSE)
Example:
R
# declaring a data frame in Rdata_frame = data.frame("Col Num  1"= c(1, 2, NA, 0),                         "Col Num  2"= c( NA, NA, 3, 8),                         "Col Num  3"= c("A", "V", "j", "y"))  print("Original data frame")print(data_frame)  # printing spaces unmodifieddata_frame_mod = data.frame("Col Num  1"= c(1, 2, NA, 0),                             "Col Num  2"= c( NA, NA, 3, 8),                             "Col Num  3"= c("A", "V", "j", "y"),                            check.names=FALSE)print("Modified data frame")print(data_frame_mod)
Output
[1] "Original data frame"
  Col.Num..1 Col.Num..2 Col.Num..3
1          1         NA          A
2          2         NA          V
3         NA          3          j
4          0          8          y
[1] "Modified data frame"
  Col Num  1 Col Num  2 Col Num  3
1          1         NA          A
2          2         NA          V
3         NA          3          j
4          0          8          y
The colnames() method in R is used to assign column names to the data frame in R. It is used to rewrite the existing values assigned to columns. It takes as input a character vector consisting of strings to be used for column names, with a length equivalent to the number of columns in R language. Since space is a valid character in the string, therefore, column name assignment made using this method accepts spaces in the names. 
Initially, the column names are converted using the make.names attribute functionality, but the values are overwritten using the assignment of string vector to the colnames() of data frame. 
Example:
R
# declaring a data frame in Rdata_frame = data.frame("Col Num  1"= c(1, 2, NA, 0),                         "Col Num  2"= c( NA, NA, 3, 8),                         "Col Num  3"= c("A", "V", "j", "y"))print("Original data frame")print(data_frame)  # defining column names using vectorcolnames(data_frame) = c("Col Num  1","Col Num  2","Col Num  3")  # printing modified data frameprint("Modified data frame")print(data_frame)
Output
[1] "Original data frame"
  Col.Num..1 Col.Num..2 Col.Num..3
1          1         NA          A
2          2         NA          V
3         NA          3          j
4          0          8          y
[1] "Modified data frame"
  Col Num  1 Col Num  2 Col Num  3
1          1         NA          A
2          2         NA          V
3         NA          3          j
4          0          8          y
names() method in R can be used as a getter or setter for any R object. It takes the object as an argument that is needed to be named and the right side is a vector with the length equivalent to the length of the object to be renamed, data frame in this case. Its usage is completely similar to colnames() method. 
Syntax:
names(df) <- c(...,..)
R
# declaring a data frame in Rdata_frame = data.frame("Col Num  1"= c(1, 2, NA, 0),                         "Col Num  2"= c( NA, NA, 3, 8),                         "Col Num  3"= c("A", "V", "j", "y"))print("Original data frame")print(data_frame)  # defining column names using vectornames(data_frame) = c("Col Num  1","Col Num  2","Col Num  3")  # printing modified data frameprint("Modified data frame")print(data_frame)
Output
[1] "Original data frame"
 Col.Num..1 Col.Num..2 Col.Num..3
1          1         NA          A
2          2         NA          V
3         NA          3          j
4          0          8          y
[1] "Modified data frame"
 Col Num  1 Col Num  2 Col Num  3
1          1         NA          A
2          2         NA          V
3         NA          3          j
4          0          8          y
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?