How to Create Empty data frame in R

Data Frame in R is a table or a two-dimensional array-like structure that is used for storing data tables. Each component of the data frame builds the column, and the contents of the component create the rows. There are several real-life scenarios in which we need to initialize an empty data frame or empty an existing data frame with or without a column and its data types. Let’s deep dive into creating an empty data frame using different ways.

Create an empty data frame in R

To create an empty data frame in R, initialize the data frame with empty vectors. Pass the empty vectors to the data.frame() function, and it will return the empty data frame.

To create a vector, use the c() function or named vectors.

df <- data.frame(name = character(),
 address = character(),
 date = as.Date(character()),
 stringsAsFactors = FALSE)

str(df)

Output

'data.frame': 0 obs. of 3 variables:
$ date: 'Date' num(0)
$ file: chr
$ user: chr

As you can see from the output that we created an empty data frame containing a character column, a date column. Here we have specified the argument stringsAsFactors = FALSE to retain the character class of our character column. The names of our data frame columns are name, address, and date.

Create an empty data frame using different column types

Initializing an empty data frame with different column types is a safer idea in the sense that you will need the correct column types from the beginning; hence if your code relies on some column type checking, it will work even with a data.frame with zero rows.

Initializing a data.frame with an empty column of the wrong type does not anticipate further insertions of rows having columns of different types.

df <- data.frame(doubles = double(),
 ints = integer(),
 factors = factor(),
 logicals = logical(),
 characters = character(),
 stringsAsFactors = FALSE)

str(df)

Output

'data.frame': 0 obs. of 5 variables:
$ doubles : num
$ ints : int
$ factors : Factor w/ 0 levels:
$ logicals : logi
$ characters: chr

In this example, we have defined different columns with different data types. For example, number valued column has integer() type, character valued column has character() type, boolean-valued column has logical() type, and factor valued column has factor() data type.

Empty an existing data frame in R

To empty an existing data frame, remove all the rows using square brackets([ ]) and pass the FALSE as the first argument.

df <- data.frame(doubles = c(1.1, 11.21),
 ints = c(1, 2),
 logicals = c(TRUE, FALSE),
 characters = c("A", "a"),
 stringsAsFactors = FALSE)

df
str(df)
cat("\n")
cat("Empty the data frame by removing all rows", "\n")
cat("\n")
empty_df <- df[FALSE,]
empty_df
str(empty_df)

Output

   doubles ints logicals characters
1   1.10    1     TRUE       A
2   11.21   2     FALSE      a
'data.frame': 2 obs. of 4 variables:
 $ doubles : num 1.1 11.2
 $ ints : num 1 2
 $ logicals : logi TRUE FALSE
 $ characters: chr "A" "a"

Empty the data frame by removing all rows

[1] doubles ints logicals characters
<0 rows> (or 0-length row.names)
'data.frame': 0 obs. of 4 variables:
 $ doubles : num
 $ ints : num
 $ logicals : logi
 $ characters: chr

We created a data frame with two rows and four columns and then emptied that data frame by removing all the rows from the data frame, and in the output, we got the empty data frame.

If you want to create a new instance with empty rows, so then this technique might be helpful to you.

Create an empty data frame without defining the column types

The simple solution for creating an empty data frame without defining the column types is to declare the data.frame().

df <- data.frame()
df
str(df)

Output

data frame with 0 columns and 0 rows
'data.frame': 0 obs. of 0 variables

Most Efficient way to create an empty data frame in R

To create an empty data frame efficiently, use the structure() method. The structure() method returns the given object with further attributes set. In short, we will use the structure() to create a list that has the class “data.frame“.

df <- structure(list(name = character(),
 address = character(),
 date = as.Date(character())),
 class = "data.frame")
df
str(df)

Output

[1] name address date
<0 rows> (or 0-length row.names)
'data.frame': 0 obs. of 3 variables:
$ name : chr
$ address: chr
$ date : 'Date' num(0)

Create empty data.frame with matrix & setNames functions

The matrix in R is a vector with attributes of a dimension and, optionally, dimension names attached to the Vector. The setNames() function sets the names on an object and returns the object. See the following code.

df <- setNames(data.frame(matrix(ncol = 3, nrow = 0)), c("name", "address", "date"))
df
str(df)

Output

[1] name address date
<0 rows> (or 0-length row.names)
'data.frame': 0 obs. of 3 variables:
$ name : logi
$ address: logi
$ date : logi

Conclusion

There are lots of ways through which you can create an empty data frame. We have seen the most basic way, then created an empty data frame using different types of columns, most efficient way using structure() method, emptied an existing data frame, and the combination of setNames() and matrix() methods.

You can use any of these as per your requirements, but my suggestion is to use the most efficient way.

That’s it for this tutorial.

See also

Create an empty Vector in R

Create an empty Matrix in R

Create an empty List in R

Leave a Comment