R – How to Create an Empty DataFrame?

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 in R.


# 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 DataFrame using data.frame()

One simple approach to create 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()
df

# Output
#data frame with 0 columns and 0 rows

Now let’s check the dimensions of the DataFrame.


# Print the dimensions
dim(df)

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

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))
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 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), ]
print(df3)

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

Related Articles

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing R – How to Create an Empty DataFrame?