How to create a basic dataframe using R?

547    Asked by SnehaPandey in Data Science , Asked on Dec 10, 2019
Answered by Sneha Pandey

A data frame is a 2 dimensional structure containing data in the form of rows and columns. A data frame is used to easily represent data in the form of spreadsheet and can be easily accessed in every algorithm. Also a dataframe contains a series of dictionaries with particular keys and values that are represented in rows and columns.

To create dataframe, we can use data.frame() function and pass vectors as arguments.

Let us build some made up weather data

days <- c('mon','tue','wed','thu','fri')

temp <- c(22.2,21,23,24.3,25)

rain <- c(TRUE, TRUE, FALSE, FALSE, TRUE)

Here days is a vector containing number of days in character form

Temp is a vector containing temperature of the particular day in double form

Rain is a vector containing logical elements of representing whether the rain occured on that day or not.

To make a dataframe with the above data, we can use the following

# Pass in the vectors:

df <- data.frame(days,temp,rain)

df





Your Answer

Interviews

Parent Categories