How to Create an Empty Data Frame R
Example 1: Creating an Empty DataFrame in R
The most basic approach to creating the empty DataFrame in R is using the data.frame() method.
print(empty_df)
dim(empty_df)
In the given code of R, we create a data.frame() function within the empty_df. The data.frame() function takes no arguments which creates a dataframe with no rows and no columns. When we print the empty_df using the print() function, we get the output of the empty DataFrame. Then, we pass the empty_df in the dim() function to get the dimensions of the dataframe as a vector with two values: the number of rows and the number of columns, respectively.
Hence, the result first displays the message that the DataFrame has “0” columns and “0” rows. Additionally, we get a vector of two zeros since the DataFrame has zero rows and zero columns.
Example 2: Creating an Empty Dataframe with “0” Column and Row in R
Another way to create the empty DataFrame is using the matrix() function and then converting it into the DataFrame. As such, both the matrix and DataFrame functions are interconvertible.
m1 = matrix(ncol = 0, nrow = 0)
df=data.frame(m1)
print ("Empty Data Frame")
print (df)
print("Data Frame Dimensions")
dim(df)
In the given code of R, we first invoke the matrix() function in the “m1” and then define the matrix() by passing the “ncol” and “nrow” parameters. The value that is assigned to these parameters is “0”. After that, we use the data.frame() function to convert the “m1” into the DataFrame. The result of the converted DataFrame is printed using the print function. The dimensions are also displayed for the DataFrame using the dim() function.
Thus, the output represents the message of the empty dataframe and the zero vector dimensions for the DataFrame due to an empty matrix.
Example 3: Creating an Empty Dataframe with N Columns in R
Moreover, we can create an empty DataFrame by specifying the column’s name using the c() function. Consider the following code of R:
df = data.frame(matrix(nrow = 0, ncol = length(cols)))
colnames(df) = cols
print(df)
We call the c() function in the “cols” character vector. Here, the c() function is specified with three column names. After that, we create an empty matrix with 0 rows and the same number of columns as the length of the cols vector within the matrix() function that is invoked inside the data.frame() function.
We pass the “nrow” with 0 value to create an empty matrix. The “ncol” is specified with length(cols) to create a matrix with the same number of columns as the length of the “cols” vector. Then, we assign the column names to the dataframe using the “colnames()” function and the “cols” vector.
As we can see, the empty dataframe with three column names is as follows:
Example 4: Creating an Empty Dataframe with an Empty Vector Assigned to Columns in R
In addition to prior approaches, we can create an empty DataFrame by specifying the empty vectors to the columns and excluding the rows. Let’s focus on the following R code to accomplish this:
c2 = integer(),
c3 = factor(),
c4 = logical(),
c5 = character(),
stringsAsFactors = FALSE)
str(df)
We declare the data.frame() function first. Then, we define the different data types for different columns inside it. Note that we pass no arguments to these data type functions to create the empty columns with no values.
Furthermore, the “stringsAsFactors” is set to FALSE to prevent R from automatically converting the character columns to factors. Then, with the str() function, the structure of the dataframe “df” is printed which includes the data types of each column and the number of rows in the dataframe.
The resulting output is displayed below the empty dataframe with five columns of different data types and no rows.
Example 5: Creating an Empty DataFrame from an Existing One in R
Conversely, if we have an existing DataFrame, we can empty it to create the empty DataFrame. We provide the following code in R for this:
Sno=c(1,2,3,4),
names=c('Alex','Candice','Jimmy','Dark'),
age=c(21, 24, 25, 26)
)
emp_df = df[FALSE,]
emp_df
We define the DataFrame which takes different column names with different types of values. Significantly, the dataframe “df” contains four rows of data here. After that, we declare a new data frame which is “emp_df” using the logical index, FALSE. This index selects no rows from the “df”. Therefore, the “emp_df” DataFrame has the same column names and data types as “df”.
The following output displays the dataframe's columns, datatypes, as well as the number of rows. Since the dataframe has zero rows, it shows the rows with zero values:
Example 6: Creating an Empty Dataframe Using the Structure() Method in R
We can make an effective use of the structure() method to generate an empty dataframe. This function gives details on a certain object with specific features. Look into the following code of R which creates an empty dataframe using the structure() function:
location = character(),
date = as.Date(character())),
class = "data.frame")
str(df)
We specify the names of the columns which hold the character() and as.Date(character()) to create an empty character and date vectors, respectively. These columns are passed inside the list() function which specifies the initial values of the columns. The structure() function here is used to create the dataframe and assign it with the "data.frame" class.
The following output represents the dataframe that has 0 observations and 3 variables and provides the names and datatypes of each variable:
Conclusion
The data.frame() function is used to create the empty DataFrame in all the given examples. We first used the data.frame() function without parameters for an empty DataFrame. Then, we created an empty DataFrame by specifying the rows and columns with zero values. We also specified the columns with the value and rows with zero, specified just the columns with values and 0 rows, and used the empty vector. Lastly, we created the empty DataFrame using the structure() function.
Source: linuxhint.com