Open In App

Create DataFrame with Spaces in Column Names in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language.

Method 1: Using check.names attribute

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 R
data_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 unmodified
data_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

Method 2 : Using colnames() method

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 R
data_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 vector
colnames(data_frame) = c("Col Num  1","Col Num  2","Col Num  3")
  
# printing modified data frame
print("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

Method 3 : Using names() method

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 R
data_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 vector
names(data_frame) = c("Col Num  1","Col Num  2","Col Num  3")
  
# printing modified data frame
print("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


Last Updated : 16 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads