What are [[ ]] used for in R?

This recipe explains what are [[ ]] used for in R

Recipe Objective

List is a data structure having components of mixed data types. We can include matrices, vectors data frames or lists in a list. R list is a container for values, its contents can be items of different data types.

Lists provide a convenient way to return the results of a statistical computation. ​

For example: Lst = list("name", list("Ram", "Shyam", "Dan")) ​

‘[‘ and ‘[[‘ are mainly used to access the elements of the lists. Hence, It is very important to distinguish Lst[[1]] from Lst[1]. ‘[[...]]’ is the operator used to select a single element, whereas ‘[...]’ is a general subscripting operator. Thus the former is the first object in the list Lst, and if it is a named list the name is not included. The latter is a sublist of the list Lst consisting of the first entry only. ​

If it is a named list, the names are transferred to the sublist. The names of components may be abbreviated down to the minimum number of letters needed to identify them uniquely. ​

This recipe demonstrates the use of "[[..]]" with an example. ​

Market Basket Analysis Project in Python with Real World Example

STEP 1: Create a list contaning sublists

Lst = list("name" = list("Ram", "Shyam", "Dan"), list(50,75,45))

STEP 2: Accessing the elements

To access the first element in the sublist named "name" ​

Lst[["name"]][1]

1. 'Ram'

This can also be accessed by using index: ​

Lst[[1]][1]

1. 'Ram'

But cannot be accessed by "[]" ​

Lst[1][1]

$name =
'Ram'
'Shyam'
'Dan''

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

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.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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