Liwaiwai Liwaiwai
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • About
Liwaiwai Liwaiwai
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • About
  • Data

Statistics For Dummies: Indexing And Subsetting In R [Part 2 of 2] : Lists And Data Frames

  • January 27, 2020
  • admin

Last time, we discussed how to index or subset vectors and matrices in R. Now, we will deal with indexing the other commonly used R objects: lists and data frames.

Typically, we will not be dealing with data with the level of simplicity of vectors and matrices. Most of the time, more structure with the information we collect. With this, most objects that you will encounter will actually come in the form of lists and data frames. Data frames are highly used especially in the context of statistical analysis.

If you are not yet familiar with R objects, you may check the introduction here.

Requirements

  • R. If you haven’t installed R yet, you may do so here. We also made a tutorial on how to install R in Ubuntu.
  • RStudio (Optional). This tutorial will use R’s IDE, RStudio. You can still this tutorial only using R.

Lists

To illustrate subsetting in lists, let’s first generate a list by running the following code:

a <- list(names = c(“Mary”,”Lucas”,”June”),
          items = c(“Pen”, “Paper”, “Stone”, “Scissors”),
          numbers = c(1:6))

 

Calling the variable, a,  shows the output below:

> a
$names
[1] "Mary"  "Lucas"  "June" 

$items
[1] "Pen"  "Paper"  "Stone"  "Scissors"

$numbers
[1] 1 2 3 4 5 6

 

There are two levels of subsetting in lists:

  • element-wise
  • within elements

Lists: Element -wise subsetting

The elements could be vectors, numbers, data frames, matrices, and others. Suppose you want to extract just the collection of items in the list, a.

The collection of items is the second element in the list. You can extract it using the bracket operator as you would in vectors:

> a[2]
$items
[1] "Pen"  "Paper"  "Stone"  "Scissors"

 

Read More  The 5 Best Job Choices In AI By Salary And Job Prospects

You can also do the same thing using the dollar ($) operator, now specifying the name of the list element as shown below:

> a$items
$items
[1] "Pen"  "Paper"  "Stone"  "Scissors"

 

Lists: Subsetting within elements

If instead, you want to extract the third item in the item list, you have to create another layer of subsetting. This can be done using both the bracket and dollar operator as shown below:

> a[[2]][3]
$items
[1] "Stone"

> a$items[3]
$items
[1] "Stone"

 

Using the bracket operator, you have to specify which element you will be extracting from by enclosing the index with double brackets ( [[ ]] ). Then, you have to specify the index of the element you are trying to extract with a bracket operator as you would in regular vectors.

Using the dollar operator, you instead have to specify the name of the element and layer it with a single bracket operator enclosing the index (or indices) of the sub-elements that you are trying to extract.

Data Frames

Data frames are extracted pretty much the same way as lists, though there are slight differences. Let’s create a data frame and see for ourselves:

b <- data.frame( A = 1:4, B = 5:8, row.names = c("Mary", "Lucas", "Mattie", "June"))

> b
       A B
Mary   1 5
Lucas  2 6
Mattie 3 7
June   4 8

Now, let’s look at how subsetting occurs in data frames.

Data Frames: Element -wise subsetting

You can use either the bracket operator or dollar operator to get the vector you desire. For instance, if we want to extract the second vector of the data frame:

> b$B
[1] 5 6 7 8

#if you want to extract the column retaining the names
> b[2]
       B
Mary   5
Lucas  6
Mattie 7
June   8

#if you want to extract the column as a vector
> b[[2]]
[1] 5 6 7 8

 

Read More  How Your Company Catches The AI Wave(s)

Data Frames: Subsetting within elements

If you want to get the fourth element in the second column, there are multiple ways to do so using either the bracket or dollar operator:

> b$B[4]
[1] 8

#treating the data as if it was a matrix:
b[4,2]
[1] 8

#treating the data as if it was a vector
> b[[2]][4]
[1] 8

Since data frames are like structured matrices, you can use matrix indices to subset within an element. You can also treat them as vectors and use a bracket operator to extract a particular element.

Conclusion

This wraps up our tutorial on how to subset or index commonly used R objects. You can explore R yourself so that you can identify what subsetting methods and what kind of objects you are most comfortable with.

admin

Related Topics
  • Data Frames
  • Lists
  • R Programming
  • Statistics
  • Subsets
You May Also Like
View Post
  • Artificial Intelligence
  • Data
  • Data Science
  • Machine Learning
  • Technology

Google Data Cloud & AI Summit : In Less Than 12 Hours From Now

  • March 29, 2023
View Post
  • Data
  • Machine Learning
  • Platforms

Coop Reduces Food Waste By Forecasting With Google’s AI And Data Cloud

  • March 23, 2023
View Post
  • Data
  • Engineering

BigQuery Under The Hood: Behind The Serverless Storage And Query Optimizations That Supercharge Performance

  • March 22, 2023
View Post
  • Data
  • Design
  • Engineering
  • Tools

Sumitovant More Than Doubles Its Research Output In Its Quest To Save Lives

  • March 21, 2023
View Post
  • Data
  • Platforms
  • Technology

How Osmo Is Digitizing Smell With Google Cloud AI Technology

  • March 20, 2023
View Post
  • Data
  • Engineering
  • Tools

Built With BigQuery: How Sift Delivers Fraud Detection Workflow Backtesting At Scale

  • March 20, 2023
View Post
  • Data

Understand And Trust Data With Dataplex Data Lineage

  • March 17, 2023
View Post
  • Big Data
  • Data

The Benefits And Core Processes Of Data Wrangling

  • March 17, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay Connected!
LATEST
  • 1
    Unlocking The Secrets Of ChatGPT: Tips And Tricks For Optimizing Your AI Prompts
    • March 29, 2023
  • 2
    Try Bard And Share Your Feedback
    • March 29, 2023
  • 3
    Google Data Cloud & AI Summit : In Less Than 12 Hours From Now
    • March 29, 2023
  • 4
    Talking Cars: The Role Of Conversational AI In Shaping The Future Of Automobiles
    • March 28, 2023
  • 5
    Document AI Introduces Powerful New Custom Document Classifier To Automate Document Processing
    • March 28, 2023
  • 6
    How AI Can Improve Digital Security
    • March 27, 2023
  • 7
    ChatGPT 4.0 Finally Gets A Joke
    • March 27, 2023
  • 8
    Mr. Cooper Is Improving The Home-buyer Experience With AI And ML
    • March 24, 2023
  • 9
    My First Pull Request At Age 14
    • March 24, 2023
  • 10
    The 5 Podcasts To Check If You Want To Get Up To Speed On AI
    • March 24, 2023

about
About
Hello World!

We are liwaiwai.com. Created by programmers for programmers.

Our site aims to provide materials, guides, programming how-tos, and resources relating to artificial intelligence, machine learning and the likes.

We would like to hear from you.

If you have any questions, enquiries or would like to sponsor content, kindly reach out to us at:

[email protected]

Live long & prosper!
Most Popular
  • 1
    GPT-4 : The Latest Milestone From OpenAI
    • March 24, 2023
  • 2
    Ditching Google: The 3 Search Engines That Use AI To Give Results That Are Meaningful
    • March 23, 2023
  • 3
    Peacock: Tackling ML Challenges By Accelerating Skills
    • March 23, 2023
  • 4
    Coop Reduces Food Waste By Forecasting With Google’s AI And Data Cloud
    • March 23, 2023
  • 5
    Gods In The Machine? The Rise Of Artificial Intelligence May Result In New Religions
    • March 23, 2023
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
  • About

Input your search keywords and press Enter.