How to create an empty DataFrame in R ?
Last Updated :
07 Apr, 2021
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 convert it to a data frame
A data frame and matrix are easily inter-convertible to each other, we first create a matrix with both rows and columns equivalent to 0 and then convert it to a data frame. The dimensions of the equivalent data frame are 0. Time complexity incurred in the creation of an empty data frame is O(1), since a constant time is required.
R
# creating a matrix with 0 rows
# and columns
mat = matrix(ncol = 0, nrow = 0)
# converting the matrix to data
# frame
df=data.frame(mat)
print ("Data Frame")
print (df)
# check for the dimensions of data
# frame
print("Dimensions of data frame")
dim(df)
Output
[1] "Data Frame"
data frame with 0 columns and 0 rows
[1] "Dimensions of data frame"
[1] 0 0
The data frame can also be created using this method, without specifying the column types. Naming the columns of a data frame is also optional. An empty vector can be passed as an argument in the data.frame method.
R
# declaring an empty data frame with 5
# columns and null entries
df = data.frame(matrix(
vector(), 0, 5, dimnames=list(c(), c("C1","C2","C3","C4","C5"))),
stringsAsFactors=F)
# printing the empty data frame
print ("Empty dataframe")
print (df)
Output
[1] "Empty dataframe"
[1] C1 C2 C3 C4 C5
<0 rows> (or 0-length row.names)
Method 2: Assign the column with the empty vectors
An empty data frame can also be created with or without specifying the column names and column types to the data values contained within it. data.frame() method can be used to create a data frame, and we can assign the column with the empty vectors. Column type checking with zero rows is supported by the data frames, where in we define the data type of each column prior to its creation.
R
# declaring an empty data frame
df <- data.frame(Col1 = double(),
Col2 = integer(),
Col3 = character(),
stringsAsFactors = FALSE)
# print the data frame
str(df)
Output
'data.frame': 0 obs. of 3 variables:
$ Col1: num
$ Col2: int
$ Col3: chr
Column 1 of the data frame can support numeric values, Column 2 can support integer values, and Column 3 as character variable values.
Similar Reads
How to Add an Empty Column to DataFrame in R? In this article, we will discuss how to add an empty column to the dataframe in R Programming Language. Add One Empty Column to dataframe Here we are going to add an empty column to the dataframe by assigning column values as NA. Syntax: dataframe[ , 'column_name'] = NA where, dataframe  is the inpu
1 min read
How to create an empty matrix in R ? The term empty matrix has no rows and no columns. A matrix that contains missing values has at least one row and column, as does a matrix that contains zeros. In this article, we are going to see how to create an empty matrix in R Programming Language. There are three ways of creating an empty matri
2 min read
How to create a DataFrame from given vectors in R ? In this article we will see how to create a Dataframe from four given vectors in R. To create a data frame in R using the vector, we must first have a series of vectors containing data. The data.frame() function is used to create a data frame from vector in R. Syntax: data.frame(vectors) Example 1.
2 min read
How to add dataframe to dataframe in R ? In this article, we will see how to add dataframe at the end of another dataframe in R Programming Language. Method 1: Using rbind() method The rbind() method in R works only if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a different num
4 min read
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
Create DataFrame Row by Row in R In this article, we will discuss how to create dataframe row by row in R Programming Language. Method 1: Using for loop and indexing methods An empty data frame in R language can be created using the data.frame() method in R. For better clarity, the data types of the columns can be defined during th
4 min read