Dataframes are data-objects in R which are combination of vectors of same length. It is represented as a two-dimensional array or a table where columns represent variables of the dataset while rows are the observations in it. Unlike matrices, dataframes contains different datatypes.
Often dataframes are created by loading a dataset from existing storage like an excel file, csv file etc. But we can also create a dataframe using a list of vectors in R. This recipe demonstrates how to create a dataframe using vectors.
We are going to take an example of student dataset which has variables like marks, name, ID. To create this dataframe, we will first create three vectors named "marks", "ID" and "name".
Note: the length of each vector has to be same
ID = c(1,2,3,4) name = c('Tom', "Harry", "David", "Daniel") marks = c(50,60,35,95)
We use data.frame() function to create a dataframe using a list of vectors.
Syntax: data.frame(df, stringAsFactors)
student = data.frame(ID,name, marks) student
1 Tom 50 2 Harry 60 3 David 35 4 Daniel 95