R Program to create an empty data frame


February 7, 2023, Learn eTutorial
1172

How to create an empty data frame using the R program?

Here we explain how to write an R program to create an empty data frame. For that, we are using a built-in function data.frame() for creating it. A data frame is used for storing data tables which have a list of vectors with equal length. The data frames are created by function data.frame(), which has tightly coupled collections of variables.

In this R program, we directly give the values to the built-in function. And print the function result. In this program, we are using the variable 'Df' that is used as a data frame. The data frame consists of different data types which are integers, Doubles, Characters, Logical, Factors, and Strings As Factors.

ALGORITHM

STEP 1: Consider variable Df as a data frame.

STEP 2: Call data.frame() with different data types

STEP 3: Assign Df with the function result

STEP 4: print the result of the function

 

R Source Code

                                          Df = data.frame(Ints=integer(),
                Doubles=double(),
                Characters=character(),
                Logicals=logical(),
                Factors=factor(),
                stringsAsFactors=FALSE)
print("Structure of the empty dataframe:")
print(str(Df))

                                      

OUTPUT

[1] "Structure of the empty dataframe:"
'data.frame': 0 obs. of  5 variables:
 $ Ints      : int 
 $ Doubles   : num 
 $ Characters: chr 
 $ Logicals  : logi 
 $ Factors   : Factor w/ 0 levels: 
NULL