Create empty DataFrame with only column names in R
Last Updated :
21 Dec, 2023
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 Names
R
# Define the data
id <- c(1, 2, 3)
names <- c("Vipul", "Jayesh", "Shivang")
address <- c("123 Main St", "456 Oak St", "789 Pine St")
phone <- c("555-1111", "555-2222", "555-3333")
aadhaar_no <- c("123-456-789", "456-789-123", "789-123-456")
# Combine vectors into a data frame
myData <- data.frame(
ID = id,
Names = names,
Address = address,
Phone = phone,
`Aadhaar No` = aadhaar_no
)
# Display the data frame
print(myData)
Output:
ID Names Address Phone Aadhaar.No
1 1 Vipul 123 Main St 555-1111 123-456-789
2 2 Jayesh 456 Oak St 555-2222 456-789-123
3 3 Shivang 789 Pine St 555-3333 789-123-456
Create Data Frame with Column Names from Matrix
R
# Create a matrix
Matrix <- matrix(1:12, nrow = 4, ncol = 3)
# Define column names
col_names <- c("ID", "Names", "Scores")
# Convert the matrix to a data frame with column names
myData <- data.frame(matrix = Matrix)
colnames(myData) <- col_names
# Display the data frame
print(myData)
Output:
ID Names Scores
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
Create empty DataFrame with only column names in R
Syntax: data.frame(input_data,nrow,ncol)
Parameter:
- input_data may be values of list or vector.
- nrow specifies the number of rows
- ncol specifies the number of columns.
Steps -
- Create an empty dataframe
- Define the column names to a variable
- Assign that variable to the dataframe.
- Display data frame so created
We can assign column names to dataframe by using colnames()
Syntax:
colnames(dataframe_name)
Given below is the implementation using the above approach.
R
# created vector with 5 characters
columns= c("id","names","address","phone","aadhaar no")
# pass this vector length to ncol parameter
# and nrow with 0
myData = data.frame(matrix(nrow = 0, ncol = length(columns)))
# assign column names
colnames(myData) = columns
# display
print(myData)
Output:
[1] id names address phone aadhaar no
<0 rows> (or 0-length row.names)
If we specify nrow parameter with morethan 0, it will take NA as that many rows.
R
# created vector with 5 characters
columns= c("id","names","address","phone","aadhaar no")
# pass this vector length to ncol parameter
# and nrow with 1
myData = data.frame(matrix(nrow=1, ncol = length(columns)))
# assign column names
colnames(myData) = columns
# display
print(myData)
# pass this vector length to ncol parameter and
# nrow with 6
myData = data.frame(matrix(nrow=6, ncol = length(columns)))
# assign column names
colnames(myData) = columns
# display
print(myData)
Output:
id names address phone aadhaar no
1 NA NA NA NA NA
2 NA NA NA NA NA
3 NA NA NA NA NA
4 NA NA NA NA NA
5 NA NA NA NA NA
6 NA NA NA NA NA
Similar Reads
Create DataFrame with Spaces in Column Names in R 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, wh
4 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 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
How to create an empty DataFrame in R ? In this article, we are going to see how to create an empty DataFrame in R Programming Language. An empty data frame corresponds to the tabular structure where the axes are of length 0, that is it does not contain any data items. Method 1: We first create a matrix with both rows and columns and then
3 min read
Select Only Numeric Columns from DataFrame in R In this article, we will discuss how to select only numeric columns from dataframe in R Programming Language. Method 1: Using Dplyr package We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric colu
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