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

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

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

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 a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.