You are currently viewing Create Empty DataFrame in R

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

Advertisements

Empty data frame in R, Pandas data frame, or PySPark data frame 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 Creating 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 data frame in the R programming language is by using the data.frame() method without any parameters. 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 data frame
df = data.frame()
df

# Output
#data frame with 0 columns and 0 rows

Now let’s check the dimensions of the data frame.


# Print the dimensions
dim(df)

# Output
#[1] 0 0

3. Create an Empty DataFrame using matrix()

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

3.1 Create an Empty DataFrame with no Rows and Columns

You can also use matric() function to create an empty data frame in R without columns. First, create a matrix() with nrow=0 rows and ncol=0 which creates an empty matrix and assigns 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) 
df
dim(df)

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 data frame 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
print(df)

Yields below output. Notice that our R data frame 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 data frame with just columns and no rows by using data.frame() and by assigning zero-length vector variables as params. This creates a datfa rame with columns of specific data types.


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

# 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 to create an empty data frame with names.


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

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

6. Create DataFrame with Just Columns from the Existing

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


# Create a data frame
df <- data.frame(
  id=c(10,11,12,13),
  name=c('sai','ram','deepika','sahithi'),
  dob=as.Date(c('1990-10-02','1981-3-24','1987-6-14','1985-8-16'))
)

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

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 data frame with 0 rows and 0 columns in R and also learned to create it by assigning names and data types using several examples.

References