How to combine 2 lists to create a dataframe in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

0

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

Relevant Projects

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.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

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.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.