To create an empty data frame in R, specify the column names and setting the number of rows to 0 using the data.frame() function.
df <- data.frame(name = character(0), address = character(0), date = as.Date(character(0)), stringsAsFactors = FALSE) str(df)
'data.frame': 0 obs. of 3 variables: $ date: 'Date' num(0) $ file: chr $ user: chr
As you can see from the output, we created an empty data frame containing a character column and a date column. Here, we specified the argument stringsAsFactors = FALSE to retain the character class of our character column. Our data frame columns are name, address, and date.
Method 2: Create an empty data frame using different column types
Initializing an empty data frame with different column types is a safer idea because 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)
'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, the number-valued column has an integer() type, the character-valued column has a character() type, the boolean-valued column has a logical() type, and factor valued column has a factor() data type.
Method 3: 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)
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  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.
Method 4: Create an empty data frame without defining the column types
The simple solution for creating an empty data frame without specifying the column types is to declare the data.frame().
df <- data.frame() df str(df)
data frame with 0 columns and 0 rows 'data.frame': 0 obs. of 0 variables
Method 5: 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 with the class “data.frame“.
df <- structure(list(name = character(), address = character(), date = as.Date(character())), class = "data.frame") df str(df)
 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)
Method 6: Creating an 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)
 name address date <0 rows> (or 0-length row.names) 'data.frame': 0 obs. of 3 variables: $ name : logi $ address: logi $ date : logi
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.