Related Articles
How to create an empty DataFrame in R ?
• Last Updated : 07 Apr, 2021

In this article, we are going to see how to create an empty DataFrame in R Programming Language. An empty data frame corresponds to the tabular structure where the axes are of length 0, that is it does not contain any data items.

Method 1: We first create a matrix with both rows and columns and then convert it to a data frame

A data frame and matrix are easily inter-convertible to each other, we first create a matrix with both rows and columns equivalent to 0 and then convert it to a data frame. The dimensions of the equivalent data frame are 0. Time complexity incurred in the creation of an empty data frame is O(1), since a constant time is required.

## R

 `# creating a matrix with 0 rows ``# and columns ``mat = ``matrix``(ncol = 0, nrow = 0)`` ` `# converting the matrix to data ``# frame``df=``data.frame``(mat)``print ``(``"Data Frame"``)``print ``(df)`` ` `# check for the dimensions of data ``# frame``print``(``"Dimensions of data frame"``)``dim``(df)`

Output

```[1] "Data Frame"
data frame with 0 columns and 0 rows
[1] "Dimensions of data frame"
[1] 0 0```

The data frame can also be created using this method, without specifying the column types. Naming the columns of a data frame is also optional. An empty vector can be passed as an argument in the data.frame method.

## R

 `# declaring an empty data frame with 5``# columns and null entries``df = ``data.frame``(``matrix``(``  ``vector``(), 0, 5, dimnames=``list``(``c``(), ``c``(``"C1"``,``"C2"``,``"C3"``,``"C4"``,``"C5"``))),``                ``stringsAsFactors=F)`` ` `# printing the empty data frame``print ``(``"Empty dataframe"``)``print ``(df)`

Output

```[1] "Empty dataframe"
[1] C1 C2 C3 C4 C5
<0 rows> (or 0-length row.names)```

Method 2: Assign the column with the empty vectors

An empty data frame can also be created with or without specifying the column names and column types to the data values contained within it. data.frame() method can be used to create a data frame, and we can assign the column with the empty vectors. Column type checking with zero rows is supported by the data frames, where in we define the data type of each column prior to its creation.

## R

 `# declaring an empty data frame``df <- ``data.frame``(Col1 = ``double``(),``                 ``Col2 = ``integer``(),``                 ``Col3 = ``character``(),``                 ``stringsAsFactors = ``FALSE``)`` ` `# print the data frame``str``(df)`

Output

```'data.frame': 0 obs. of  3 variables:
\$ Col1: num
\$ Col2: int
\$ Col3: chr ```

Column 1 of the data frame can support numeric values, Column 2 can support integer values, and Column 3 as character variable values.

My Personal Notes arrow_drop_up