4 Ways to Create Blank Data Frame with R

Deepanshu Bhalla 7 Comments

This tutorial explains various ways to create an empty data frame with R.

Method 1: Create an empty data frame

blank_df <- data.frame()

Method 2: Create an empty data frame using matrix and convert to data.frame

blank_df <- data.frame(matrix(ncol = 0, nrow = 0))

Method 3: Creating an empty data frame with structure()

empty_df <- structure(list(), class = "data.frame")

Method 4: Create an empty data frame using tibble

library(dplyr)
empty_df <- tibble()

To check the number of rows and columns in a dataframe, you can use the function dim()

dim(empty_df)

It returns 0 0 for empty_df which means 0 number of rows and columns.

Specifying column names and data types

The following code creates an empty data frame with column names and data types.

empty_df <- data.frame(Column1 = numeric(0), 
                       Column2 = character(0), 
                       Column3 = logical(0))

You can also specify the column names like the code below to create an empty data frame.

empty_df <- data.frame(matrix(ncol = 3, nrow = 0))
col_names <- c("Column1", "Column2", "Column3")
names(empty_df) <- col_names
Practical Application

Creating an empty data frame before entering a loop can be very useful, especially when you plan to append data or results from each iteration of the loop. This practice ensures that you have a pre-defined structure in place to which you can add data in subsequent iterations.

By creating an empty data frame before the loop, you define the column names, data types, and other necessary attributes of the data frame. This helps avoid potential issues with mismatched data types or columns that might arise if you directly add data to the data frame within the loop without predefining its structure. Check out the code below.

logitfn = function(df,depvar) {
  dummydt=data.frame(matrix(ncol=0,nrow=0))
  depvar1 = deparse(substitute(depvar))
  xxxx = names(df)[which(names(df)!= depvar1)]
  for (i in 1:length(xxxx)) {
    mylogit = glm(formula(paste(depvar1,"~",xxxx[i])), data = df, family = "binomial")
    coeff = data.frame(summary(mylogit)$coefficient)
    if (i==1) {
      output = rbind(dummydt,coeff)
    }  
    else {
      output = rbind(output,coeff)
    }  
  }
  
  return(output)
}

# Convert the target variable "Species" to binary
iris$is_setosa <- ifelse(iris$Species == "setosa", 1, 0)
iris <- iris[, !(names(iris) %in% "Species")]
outdata <- logitfn(iris, is_setosa)
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

7 Responses to "4 Ways to Create Blank Data Frame with R"
  1. > dummydt=data.frame(matrix(ncol=0,nrow=0))
    > unilogit2 = function(df,depvar, output) {
    + dummydt=data.frame(matrix(ncol=0,nrow=0)) depvar1 = deparse(substitute(depvar))
    Error: unexpected symbol in:
    "unilogit2 = function(df,depvar, output) {
    dummydt=data.frame(matrix(ncol=0,nrow=0)) depvar1"
    > out = deparse(substitute(output))
    > xxxx = names(df)[which(names(df)!= depvar1)]
    Error in which(names(df) != depvar1) : object 'depvar1' not found
    > for (i in 1:length(xxxx)) {
    + mylogit = glm(formula(paste(depvar1,"~",xxxx[i])), data = df, family = "binomial")
    + coeff = data.frame(summary(mylogit)$coefficient)
    + if (i==1) {output = rbind(dummydt,coeff)} else {output = rbind(output,coeff)} assign(out,output, envir = .GlobalEnv)
    Error: unexpected symbol in:
    " coeff = data.frame(summary(mylogit)$coefficient)
    if (i==1) {output = rbind(dummydt,coeff)} else {output = rbind(output,coeff)} assign"
    > }
    Error: unexpected '}' in " }"
    >

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
Next → ← Prev