<- c("first" = "Leia", "last" = "Organa")
name
<- c("height" = 150, "weight" = 49)
body
<- TRUE
force
<- "Alderaan" home
Practice: Lists
Stat 133
- 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:
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:
<- list(
leia "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:
<- list(
leia2
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:
<- list("first" = "Leia", "last" = "Organa")
name 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:
<- list(
leia "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
= 5:1
vec 1:3]
vec[
# subsetting a matrix
= matrix(seq(2, 12, by = 2), nrow = 2, ncol = 3)
mat 2, c(1,3)]
mat[
# subsetting a 3-dim array
= array(1:12, dim = c(2, 2, 3))
arr 1:2, 2, 3] arr[
You can also use double brackets to subset a single element of an atomic object:
# first element in vec
1]]
vec[[
# fifth element in mat
5]] mat[[
If you try to subset more than one element using double brackets, you’ll get a nice error message:
# this fails
1:2]] vec[[
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
:
1]
leia[
1:2]
leia[
-1]
leia[
c(4, 2)]
leia[
"name"]
leia[
c("home", "force")]
leia[
c(FALSE, TRUE, FALSE, TRUE)] leia[
Now inspect the next set of commands that use double brackets:
1]]
leia[[
2]]
leia[[
"name"]]
leia[[
"unknown"]] leia[[
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:
$"name"
leia
$home leia
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.
<- c('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune')
planet
<- c(0.33, 4.87, 5.97, 0.642, 1898, 568, 86.8, 102)
mass
<- c(167L, 464L, 15L, -65L, -110L, -140L, -195L, -200L)
temperature
<- c(0L, 0L, 1L, 2L, 79L, 62L, 27L, 14L)
moons
<- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE) rings
3.1 solar
list
- Use these vectors to create a list
solar
. When printed, yoursolar
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
<- list(
solar 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):
- What is the name of the lightest planet? Hint: the
which.min()
function is your friend.
Show answer
$planet[which.min(solar$mass)] solar
- What is the name of the heaviest planet? Hint: the
which.max()
function is your friend.
Show answer
$planet[which.max(solar$mass)] solar
- What is the temperature of the planet with the most number of moons? Hint: the
which.max()
function is your friend.
Show answer
$temperature[which.max(solar$moons)] solar
- What is the mass of the planet with one moon?
Show answer
$mass[solar$moons == 1] solar
- How many planets have temperatures less than or equal to zero?
Show answer
sum(solar$temperature <= 0)
- 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)
- What is the name of the planet whose mass is furthest from the average mass (of all planets)?
Show answer
$planet[which.max(abs(solar$mass - mean(solar$mass)))] solar