Practice: Lists

Stat 133

Author

Gaston Sanchez

Learning Objectives
  • Create Lists
  • Subset (subscript) lists with double brackets [[]]
  • Subset (subscript) named elements with $

1 R Lists

Consider the following information about Leia Organa:

Leia Organa is a force-sensitive woman, 150 cm tall, and weighs 49 kgs. She grew up in Alderaan, and she is a princess.

Leia’s information can be rearranged into an itemized structure such as:

- name
  + first: Leia
  + last: Organa
- body measurements
  + height: 150
  + weight: 49
- force sensitive: True 
- homeworld: Alderaan

In R, how can we store Leia’s information? One option is to create vectors, for instance:

name <- c("first" = "Leia", "last" = "Organa")

body <- c("height" = 150, "weight" = 49)

force <- TRUE

home <- "Alderaan"

1.1 Creating lists with list()

Because all these vectors refer to Leia, we can take a step further and use an R list to put them all in one place:

leia <- list(
  "name" = name,
  "body" = body,
  "force" = force,
  "home" = home
)

leia
$name
   first     last 
  "Leia" "Organa" 

$body
height weight 
   150     49 

$force
[1] TRUE

$home
[1] "Alderaan"

Notice how R display the contents of a list in which its elements have names.

1.2 List with unnamed elements

To create a list use the function list(). You can pass any number of objects inside lists, separated by comma. Naming elements in a list is optional. This means that we could also create a list for Leia as follows:

leia2 <- list(
  name,
  body,
  force,
  home
)

leia2
[[1]]
   first     last 
  "Leia" "Organa" 

[[2]]
height weight 
   150     49 

[[3]]
[1] TRUE

[[4]]
[1] "Alderaan"

The lists leia and leia2 store the same vectors, in the same order. The only difference is that leia has named elements, whereas leia2 has unnamed elements. Whenever possible, I highly recommend giving names to the elements in a list because this makes it easier to understand which elements are being manipulated referring to them by their names.

1.3 Lists are generic objects

R lists are the most general class of data object in R. Any other object can be an element of a list, even other lists.

For instance, let’s implement name as a list instead of a vector:

name <- list("first" = "Leia", "last" = "Organa")
name
$first
[1] "Leia"

$last
[1] "Organa"

And now let’s create a new list leia in which the first element, name, is the previously created list:

leia <- list(
  "name" = name,
  "body" = body,
  "force" = force,
  "home" = home
)

leia
$name
$name$first
[1] "Leia"

$name$last
[1] "Organa"


$body
height weight 
   150     49 

$force
[1] TRUE

$home
[1] "Alderaan"

2 Subsetting Lists

The way you typically subset atomic objects is with bracket notation using single brackets. Depending on the dimensions of the object, inside brackets you pass one or more indexing vectors:

# subsetting a vector
vec = 5:1
vec[1:3]

# subsetting a matrix
mat = matrix(seq(2, 12, by = 2), nrow = 2, ncol = 3)
mat[2, c(1,3)]

# subsetting a 3-dim array
arr = array(1:12, dim = c(2, 2, 3))
arr[1:2, 2, 3]

You can also use double brackets to subset a single element of an atomic object:

# first element in vec
vec[[1]]

# fifth element in mat
mat[[5]]

If you try to subset more than one element using double brackets, you’ll get a nice error message:

# this fails
vec[[1:2]]
Error in vec[[1:2]]: attempt to select more than one element in vectorIndex

2.1 What about subsetting lists?

R lists admit three kinds of subsetting:

  • with single brackets: lis[2]

  • with double brackets: lis[[2]]

  • with dollar sign (on a single named element): lis$name

Run the following commands and try to understand how R is subsetting the list leia:

leia[1]

leia[1:2]

leia[-1]

leia[c(4, 2)]

leia["name"]

leia[c("home", "force")]

leia[c(FALSE, TRUE, FALSE, TRUE)]


Now inspect the next set of commands that use double brackets:

leia[[1]]

leia[[2]]

leia[["name"]]

leia[["unknown"]]


When one or more elements of a list are named, you can also use the dollar $ operator to subset a single element by referring to its name. Quoting the name is optional:

leia$"name"

leia$home

Reflect on the similarities and differences of the various syntax in which the elements of a list can be subset.


3 Planets

Here are some vectors with data about the planets of the Solar system.

planet <- c('Mercury',  'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune')

mass <- c(0.33, 4.87, 5.97, 0.642, 1898, 568, 86.8, 102)

temperature <- c(167L, 464L, 15L, -65L, -110L, -140L, -195L, -200L)

moons <- c(0L, 0L, 1L, 2L, 79L, 62L, 27L, 14L)

rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)

3.1 solar list

  1. Use these vectors to create a list solar. When printed, your solar list should be displayed as:
$planet
[1] "Mercury" "Venus"   "Earth"   "Mars"    "Jupiter" "Saturn"  "Uranus"  "Neptune"

$mass
[1]    0.330    4.870    5.970    0.642 1898.000  568.000   86.800  102.000

$temperature
[1]  167  464   15  -65 -110 -140 -195 -200

$moons
[1]  0  0  1  2 79 62 27 14

$rings
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
Show answer
# solar list
solar <- list(
  planet = planet,
  mass = mass,
  temperature = temperature,
  moons = moons,
  rings = rings
)

3.2 Manipulation of solar

Use the list solar to write R commands—displaying the output—that answer the following questions (use only the list solar, NOT the individual vectors):

  1. What is the name of the lightest planet? Hint: the which.min() function is your friend.
Show answer
solar$planet[which.min(solar$mass)]
  1. What is the name of the heaviest planet? Hint: the which.max() function is your friend.
Show answer
solar$planet[which.max(solar$mass)]
  1. What is the temperature of the planet with the most number of moons? Hint: the which.max() function is your friend.
Show answer
solar$temperature[which.max(solar$moons)]
  1. What is the mass of the planet with one moon?
Show answer
solar$mass[solar$moons == 1]
  1. How many planets have temperatures less than or equal to zero?
Show answer
sum(solar$temperature <= 0)
  1. What is the 80th percentile of temperature for planets that have no rings? Hint: the quantile() function is your friend.
Show answer
quantile(solar$temperature[solar$rings == FALSE], 
         probs = 0.80)
  1. What is the name of the planet whose mass is furthest from the average mass (of all planets)?
Show answer
solar$planet[which.max(abs(solar$mass - mean(solar$mass)))]