R Program to Create an Empty Dataframe

Example 1: Create an Empty Dataframe in R

# create empty dataframe
empty_dataframe <- data.frame()

# display empty_dataframe
print(empty_dataframe)

Output

data frame with 0 columns and 0 rows

In the above example, we have created an empty dataframe named empty_dataframe using the data.frame() function.

Since the dataframe we created is empty, we get data frame with 0 columns and 0 rows as an output.


Example 2: Initialize Empty Vector to Create Empty Dataframe in R

# create dataframe with 5 empty vectors
empty_dataframe <- data.frame(Doubles = double(),
  Characters = character(),
  Integers = integer(),
  Logicals = logical(),
  Factors = factor()
)

# display structure of empty_dataframe
print(str(empty_dataframe))

Output

'data.frame':	0 obs. of  5 variables:
 $ Doubles   : num 
 $ Characters: chr 
 $ Integers  : int 
 $ Logicals  : logi 
 $ Factors   : Factor w/ 0 levels: 
NULL

Here, we have defined the data frame named empty_dataframe as a set of empty vectors with specific class types.

We have used the str() function to see the structure of empty_dataframe.

After displaying the structure of the empty dataframe, we can see that the dataframe has 0 observations (i.e. rows), 5 variables (i.e. columns), and each of the variables are five different classes.