Create DataFrame with Spaces in Column Names in R
Last Updated :
16 May, 2021
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
Similar Reads
Create empty DataFrame with only column names in R
In this article, we are going to discuss how to create a data frame in r with column names and create an empty data frame with column names in the R Programming Language. The basic syntax for creating a data frame is using data. frame(). Create a Data Frame with Values and column NamesR # Define the
3 min read
Replace Spaces in Column Names in R DataFrame
In this article, we will replace spaces in column names of a dataframe in R Programming Language. Let's create a Dataframe with 4 columns with 3 rows: R # create a dataframe with 4 columns and 3 rows data = data.frame("web technologies" = c("php","html","js"), "backend tech" = c("sql","oracle","mong
2 min read
Convert list to dataframe with specific column names in R
A list contains different types of objects as their components. The components may belong to different data types or different dimensions. Vector can be useful components of a list and can be easily mapped as the rows or columns of a dataframe. Each column in the dataframe is referenced using a uniq
4 min read
Convert DataFrame to Matrix with Column Names in R
Data frames and matrices are R objects, both of which allow tabular storage of the data into well organized cells. However, the data in a data frame can consist of different data types, that is the cells may contain data belonging to a combination of data types. Matrices, on the other hand, strictly
3 min read
Convert Row Names into Column of DataFrame in R
In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
3 min read
Convert dataframe column to list in R
In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter
2 min read
Convert DataFrame Column to Numeric in R
In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion,
9 min read
Combine two DataFrames in R with different columns
In this article, we will discuss how to combine two dataframes with different columns in R Programming Language. Method 1 : Using plyr package The "plyr" package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the
5 min read
How to Stack DataFrame Columns in R?
A dataframe is a tubular structure composed of rows and columns. The dataframe columns can be stacked together to divide the columns depending on the values contained within them. Method 1: Using stack method The cbind() operation is used to stack the columns of the data frame together. Initially,
3 min read
How to add suffix to column names in R DataFrame ?
Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the
4 min read