How to combine 2 lists to create a dataframe in R?

This recipe helps you combine 2 lists to create a dataframe 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. But we can also create a dataframe from vectors or lists in R. This recipe demonstrates how to create a dataframe combining 2 lists.

Step 1: Creating 2 lists

We are going to take an example of student dataset which has variables like marks and name. To create this dataframe, we will first create 2 lists named "marks" and "name".

Note: the length of each lists has to be same

name = list('Tom', "Harry", "David", "Daniel") marks = list(50,60,35,95)

Step 2: Creating a Dataframe

We use data.frame() and unlist() functions to create a dataframe using lists. unlist() function is used to covert list to vector so that we can use it as "df" argument in data.frame() function.

Syntax:

1. 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;

unlist(x, recursive = TRUE, use.names = TRUE)

where:

  1. x = lists;
  2. recursive = By defalut it's TRUE but if FALSE, the function won't recurse beyond first level of list;
  3. use.names = By default it's TRUE and its meant to preserve the naming information;

student = data.frame(unlist(name),unlist(marks)) ​ #to name the columns we use names() function names(student) = c("Name","Marks") ​ ​ student

Tom	50
Harry	60
David	35
Daniel	95

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python