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

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

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.


1. data.frame(df, stringAsFactors)


  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)


  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

Relevant Projects

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.