Create Empty DataFrame in R

There are multiple ways to create an empty DataFrame in the R programming language, I will cover first by using data.frame() without params, second using matrix(), the third is by initializing empty vectors, and the fourth is creating an DataFrame with just columns from another DataFrame.

Empty DataFrame in R, Pandas DataFrame, or PySPark DataFrame usually refers to 0 rows and 0 columns however, sometimes, you would require to have column names and specify the data types for each column, but without any rows. In this article, let’s see these with examples.

1. Quick Examples of Create Empty DataFrame in R

Following are quick examples of how to create an empty DataFrame.

# Quick Examples

# Example 1 - Using empty data.frame()
df = data.frame()

# Example 2 - Using matrix()
df = data.frame(matrix(nrow = 0, ncol = 0)) 

# Example 3 - Assign column names to empty DataFrame
# Create an Empty DataFrame with 0 rows and n columns 
columns = c("id","name","dob") 
df = data.frame(matrix(nrow = 0, ncol = length(columns))) 
colnames(df) = columns

# Example 4
# Assign columns & datatype to empty DataFrame
df = data.frame(id=numeric(0),name=character(0),dob=character(0))

# Example 5
# By Setting column variables with NA
df = data.frame(id=NA, name=NA, dob=NA)[numeric(0), ]

# Example 6
# Create empty DataFrame from existing
empty_df = df[FALSE,]

2. Create an Empty R DataFrame using data.frame()

One simple approach to creating an empty DataFrame in the R programming language is by using data.frame() method without any params. This creates an R DataFrame without rows and columns (0 rows and 0 columns). data.frame() is also used to create an R Data Frame.

# Create an Empty DataFrame
df = data.frame()

# Output
#data frame with 0 columns and 0 rows

Now let’s check the dimensions of the DataFrame.

# Print the dimensions

# Output
#[1] 0 0

3. Create an Empty DataFrame using matrix()

We are often required to create a DataFrame in R without rows and with or without columns. In this section, I will show different examples of how to initialize an empty DataFrame with and without columns.

3.1 Create Empty DataFrame with no Rows and Columns

You can also use matric() function to create an empty DataFrame in R without columns. First, create a matrix() with nrow=0 rows and ncol=0 which creates an empty matrix and assign this to data.frame() function.

# Create an Empty DataFrame with 0 rows and 0 columns
matrix_empty = matrix(nrow = 0, ncol = 0)
df = data.frame(matrix_empty) 

Yields the same output as above.

3.2 Create Empty DataFrame with Columns

To create an empty data frame with column names , initialize a vector with column names first, c() is used to create a Vector in R. And then create DataFrame by using data.frame() function and assign this vector to columns(df).

# Create a Vector with Columns
columns = c("id","name","dob") 

#Create a Empty DataFrame with 0 rows and n columns
df = data.frame(matrix(nrow = 0, ncol = length(columns))) 

# Assign column names
colnames(df) = columns

# print DataFrame

Yields below output. Notice that our R DataFrame has 0 rows with 3 columns id, name and dob.

# Output
[1] id   name dob 
<0 rows> (or 0-length row.names)

4. By Initializing Empty Vectors

You can also create a DataFrame with just columns and no rows by using data.frame() and by assigning zero-length vector variables as params. This creates a DataFrame with columns of specific data types.

# Using empty vectors
df2 = data.frame(id=numeric(0),name=character(0),dob=character(0))

# Output
#[1] id   name dob 
#<0 rows> (or 0-length row.names)

Here, id is a numeric column, name and dob are characters.

5. Using NA Variables

Also, assign NA to columns in order to create an empty DataFrame with names.

# Create Empty DataFrame with column names
df3 = data.frame(id=NA, name=NA, dob=NA)[numeric(0), ]

# Output
#[1] id   name dob 
#<0 rows> (or 0-length row.names)

6. Create DataFrame with Just Columns from Existing

Finally, if you already have an existing DataFrame, let’s say df, and wanted to create another empty DataFrame by just using the column names and types without rows, use the below approach.

# Create a DataFrame
df <- data.frame(

# Create empty DataFrame with columns from existing DataFrame.
empty_df = df[FALSE,]

Yields below output. Notice that df still contains the data, but empty_df is empty with columns id, name, and dob but, without rows.

# Output
[1] id   name dob 
<0 rows> (or 0-length row.names)

7. Conclusion

In this article, you have learned how to create an empty DataFrame with 0 rows and 0 columns in R and also learned to create it by assigning names and data types using several examples.


Naveen (NNK)

I am Naveen (NNK) working as a Principal Engineer. I am a seasoned Apache Spark Engineer with a passion for harnessing the power of big data and distributed computing to drive innovation and deliver data-driven insights. I love to design, optimize, and managing Apache Spark-based solutions that transform raw data into actionable intelligence. I am also passion about sharing my knowledge in Apache Spark, Hive, PySpark, R etc.

Leave a Reply

You are currently viewing Create Empty DataFrame in R