Liwaiwai Liwaiwai
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • Learning
  • About
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • Learning
  • About
Liwaiwai Liwaiwai
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • Learning
  • 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.


Partner with liwaiwai.com
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

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  Scaling Machine Learning Inference With NVIDIA Tensorrt And Google Dataflow

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.


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

admin

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

Applying Generative AI To Product Design With BigQuery DataFrames

  • September 21, 2023
View Post
  • Data
  • Platforms

Microsoft And Oracle Expand Partnership To Deliver Oracle Database Services On Oracle Cloud Infrastructure In Microsoft Azure

  • September 14, 2023
View Post
  • Artificial Intelligence
  • Data
  • Platforms
  • Software Engineering
  • Technology

Combining AI With A Trusted Data Approach On IBM Power To Fuel Business Outcomes

  • September 11, 2023
View Post
  • Data
  • Learning

Resources to Take Your Charts From Bland to Beautiful

  • September 7, 2023
View Post
  • Artificial Intelligence
  • Data
  • Data Science
  • Platforms

Reimagine Data Analytics For The Era Of AI

  • August 30, 2023
View Post
  • Artificial Intelligence
  • Data
  • Machine Learning
  • Platforms

IBM Introduces ‘Watsonx Your Business’

  • August 28, 2023
Google Cloud Next 2023
View Post
  • Artificial Intelligence
  • Data
  • Engineering
  • Platforms

10 Must-Attend Sessions For Data Professionals At Google Cloud Next ‘23

  • August 23, 2023
View Post
  • Data
  • Machine Learning
  • Research
  • Technology

Using Machine Learning To Help ZSL & Network Rail Monitor And Improve Biodiversity Near British Railways

  • August 21, 2023
A Field Guide To A.I.
Navigate the complexities of Artificial Intelligence and unlock new perspectives in this must-have guide.
Now available in print and ebook.

charity-water



Stay Connected!
LATEST
  • OpenAI 1
    How We Interact With Information: The New Era Of Search
    • September 28, 2023
  • 2
    Bring AI To Looker With The Machine Learning Accelerator
    • September 28, 2023
  • 3
    3 Questions: A New PhD Program From The Center For Computational Science And Engineering
    • September 28, 2023
  • 4
    Microsoft And Mercy Collaborate To Empower Clinicians To Transform Patient Care With Generative AI
    • September 27, 2023
  • 5
    NASA’s Mars Rovers Could Inspire A More Ethical Future For AI
    • September 26, 2023
  • 6
    Oracle CloudWorld 2023: 6 Key Takeaways From The Big Annual Event
    • September 25, 2023
  • 7
    3 Ways AI Can Help Communities Adapt To Climate Change In Africa
    • September 25, 2023
  • Robotic Hand | Lights 8
    Nvidia H100 Tensor Core GPUs Come To Oracle Cloud
    • September 24, 2023
  • 9
    AI-Driven Tool Makes It Easy To Personalize 3D-Printable Models
    • September 22, 2023
  • 10
    Huawei: Advancing a Flourishing AI Ecosystem Together
    • September 22, 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
  • Coffee | Laptop | Notebook | Work 1
    First HP Work Relationship Index Shows Majority of People Worldwide Have an Unhealthy Relationship with Work
    • September 20, 2023
  • 2
    Huawei Connect 2023: Accelerating Intelligence For Shared Success
    • September 20, 2023
  • 3
    Applying Generative AI To Product Design With BigQuery DataFrames
    • September 21, 2023
  • 4
    Combining AI With A Trusted Data Approach On IBM Power To Fuel Business Outcomes
    • September 21, 2023
  • Microsoft and Adobe 5
    Microsoft And Adobe Partner To Deliver Cost Savings And Business Benefits
    • September 21, 2023
  • /
  • Artificial Intelligence
  • Explore
  • About
  • Contact Us

Input your search keywords and press Enter.