What are dataframes and how to create them in R

This recipe explains what are dataframes and how to create them in R

Recipe Objective

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.

Access Product Recommendation System Project with Source Code

Step 1: Creating a list of 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)

Step 2: Creating a Dataframe

We use data.frame() function to create a dataframe using a list of vectors.

Syntax: data.frame(df, stringAsFactors)

where:

  1. df = is matrix or collection of vectors that needs to be joined;
  2. stringAsFactors = if TRUE, it converts string to vector by default;

student = data.frame(ID,name, marks) student

1	Tom	50
2	Harry	60
3	David	35
4	Daniel	95

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

OSZAR »