Course
Data Frames
Convert a List to a DataframeCreate an Empty DataframeCombine Two Dataframe into OneChange Column Name of a DataframeExtract Columns From a DataframeDrop Columns in a DataframeReorder Columns in a DataframeSplit DataframeMerge Multiple DataframesDelete Rows From DataframeMake a List of DataframesIntroduction
"Hello World" ProgramAdd Two VectorsFind Sum, Mean and Product of Vector in R ProgrammingTake Input From UserGenerate Random Number from Standard DistributionsSample from a PopulationFind Minimum and MaximumSort a VectorStrings
Concatenate Two StringsFind the Length of a StringCheck if Characters are Present in a StringExtract n Characters From a StringReplace Characters in a StringCompare two StringsConvert Factors to CharactersTrim Leading and Trailing WhitespacesVectors
Concatenate a Vector of StringsCheck if a Vector Contains the Given ElementCount the Number of Elements in a VectorFind Index of an Element in a VectorAccess Values in a VectorAdd Leading Zeros to VectorR 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.