48  46. Practice Questions - TOPIC: Intro to Lists

rm(list=ls())

# The following list contains information about a sports league
# with three basketball teams. 
#
# The first three items in the list contain general information about 
# the league: the league name, the season, and the number of games per season.
#
# The remaining items in the list are lists containing information
# about each team in the league.
#
# Each team's information includes:
#   - the team name
#   - the team's city
#   - a vector of points scored in each game they've played so far
#   - a vector of points allowed (given up to opponents) in each game
#   - a logical vector indicating whether each game was a home game (TRUE) or away game (FALSE)
#
# Answer the questions below by referring to this data. You should 
# write the code so that it will continue to work even if the actual 
# values in the data change. 

league = list(
  
  "Basketball League",
  
  "2024-2025",
  
  82,
  
  list(
    "Thunderbolts",
    "Springfield",
    c(102, 95, 110, 88, 103, 97, 105),
    c(98, 92, 105, 90, 99, 95, 102),
    c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE)
  ),
  
  list(
    "Dragons",
    "Riverside",
    c(88, 92, 85, 97, 103, 90),
    c(90, 88, 87, 95, 100, 92),
    c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE)
  ),
  
  list(
    "Eagles",
    "Lakewood",
    c(105, 98, 92, 110, 99, 101, 95, 103),
    c(100, 95, 90, 108, 97, 99, 93, 101),
    c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE)
  )
  
  # If there were more teams in the league, their data would 
  # appear as separate lists here.
)

# NOTE - the following command may help you to understand the data better
str(league)
List of 6
 $ : chr "Basketball League"
 $ : chr "2024-2025"
 $ : num 82
 $ :List of 5
  ..$ : chr "Thunderbolts"
  ..$ : chr "Springfield"
  ..$ : num [1:7] 102 95 110 88 103 97 105
  ..$ : num [1:7] 98 92 105 90 99 95 102
  ..$ : logi [1:7] TRUE TRUE FALSE FALSE TRUE FALSE ...
 $ :List of 5
  ..$ : chr "Dragons"
  ..$ : chr "Riverside"
  ..$ : num [1:6] 88 92 85 97 103 90
  ..$ : num [1:6] 90 88 87 95 100 92
  ..$ : logi [1:6] FALSE TRUE TRUE FALSE TRUE FALSE
 $ :List of 5
  ..$ : chr "Eagles"
  ..$ : chr "Lakewood"
  ..$ : num [1:8] 105 98 92 110 99 101 95 103
  ..$ : num [1:8] 100 95 90 108 97 99 93 101
  ..$ : logi [1:8] TRUE FALSE TRUE TRUE FALSE TRUE ...
#----------------------------------------------------------------------------------
# QUESTION 1
# TOPIC: lists, mode, length
# 
# PART A
# Write a command that returns the mode of the league variable.
# What is the result?
#
# PART B
# Write a command that returns the number of items in the league list.
#
# PART C
# Based on your answer from PART B, write a command that calculates 
# the number of teams in the league. Remember that the first three 
# items in the list are general league information, not team data.
#----------------------------------------------------------------------------------
###################.
# Answer - part A
###################.

mode(league)
[1] "list"
# The result is "list"
###################.
# Answer - part B
###################.

length(league)
[1] 6
# The result is 6 (3 general info items + 3 teams)
###################.
# Answer - part C
###################.

length(league) - 3
[1] 3
# We subtract 3 because the first 3 items are not team data
#----------------------------------------------------------------------------------
# QUESTION 2
# TOPIC: lists, indexing with [single-brackets]
# 
# PART A
# Write a command using [single-bracket] indexing that returns a list 
# containing ONLY the first team's data (the Thunderbolts).
# Use positive position numbers.
#
# PART B
# Write a command using mode() to verify that your answer from PART A
# returns a list.
#
# PART C
# Write a command using [single-bracket] indexing that returns a list
# containing the data for the first and third teams.
# Use positive position numbers.
#----------------------------------------------------------------------------------
###################.
# Answer - part A
###################.

league[4]
[[1]]
[[1]][[1]]
[1] "Thunderbolts"

[[1]][[2]]
[1] "Springfield"

[[1]][[3]]
[1] 102  95 110  88 103  97 105

[[1]][[4]]
[1]  98  92 105  90  99  95 102

[[1]][[5]]
[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE
# This returns a list containing the Thunderbolts data
# Note: The team data starts at position 4 because the first
# 3 positions contain general league information
###################.
# Answer - part B
###################.

mode(league[4])
[1] "list"
# The result is "list"
###################.
# Answer - part C
###################.

league[c(4, 6)]
[[1]]
[[1]][[1]]
[1] "Thunderbolts"

[[1]][[2]]
[1] "Springfield"

[[1]][[3]]
[1] 102  95 110  88 103  97 105

[[1]][[4]]
[1]  98  92 105  90  99  95 102

[[1]][[5]]
[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE


[[2]]
[[2]][[1]]
[1] "Eagles"

[[2]][[2]]
[1] "Lakewood"

[[2]][[3]]
[1] 105  98  92 110  99 101  95 103

[[2]][[4]]
[1] 100  95  90 108  97  99  93 101

[[2]][[5]]
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
# This returns a list with the Thunderbolts (position 4) 
# and Eagles (position 6)
#----------------------------------------------------------------------------------
# QUESTION 3
# TOPIC: lists, indexing with [single-brackets] using negative positions
# 
# For each part of this question you are to write a single command that
# returns a list containing all the team data but NONE of
# the general league information (league name, season, games per season).
#
# PART A
# Do the above using positive numbers for the index.
# Verify your answer by using str() on the result.
#
# PART B
# Do this again but this time, use negative numbers for the index.
#
# PART C
# Do this again but this time, use logical values (i.e. TRUE/FALSE values) for the index.
#----------------------------------------------------------------------------------
#########################################.
# Answer - with positive index numbers
#########################################.

league[4:length(league)]
[[1]]
[[1]][[1]]
[1] "Thunderbolts"

[[1]][[2]]
[1] "Springfield"

[[1]][[3]]
[1] 102  95 110  88 103  97 105

[[1]][[4]]
[1]  98  92 105  90  99  95 102

[[1]][[5]]
[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE


[[2]]
[[2]][[1]]
[1] "Dragons"

[[2]][[2]]
[1] "Riverside"

[[2]][[3]]
[1]  88  92  85  97 103  90

[[2]][[4]]
[1]  90  88  87  95 100  92

[[2]][[5]]
[1] FALSE  TRUE  TRUE FALSE  TRUE FALSE


[[3]]
[[3]][[1]]
[1] "Eagles"

[[3]][[2]]
[1] "Lakewood"

[[3]][[3]]
[1] 105  98  92 110  99 101  95 103

[[3]][[4]]
[1] 100  95  90 108  97  99  93 101

[[3]][[5]]
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
# Verifying the answer with the str function
str(league[4:length(league)])
List of 3
 $ :List of 5
  ..$ : chr "Thunderbolts"
  ..$ : chr "Springfield"
  ..$ : num [1:7] 102 95 110 88 103 97 105
  ..$ : num [1:7] 98 92 105 90 99 95 102
  ..$ : logi [1:7] TRUE TRUE FALSE FALSE TRUE FALSE ...
 $ :List of 5
  ..$ : chr "Dragons"
  ..$ : chr "Riverside"
  ..$ : num [1:6] 88 92 85 97 103 90
  ..$ : num [1:6] 90 88 87 95 100 92
  ..$ : logi [1:6] FALSE TRUE TRUE FALSE TRUE FALSE
 $ :List of 5
  ..$ : chr "Eagles"
  ..$ : chr "Lakewood"
  ..$ : num [1:8] 105 98 92 110 99 101 95 103
  ..$ : num [1:8] 100 95 90 108 97 99 93 101
  ..$ : logi [1:8] TRUE FALSE TRUE TRUE FALSE TRUE ...
#########################################.
# Answer - with negative index numbers
#########################################.
league[-(1:3)]
[[1]]
[[1]][[1]]
[1] "Thunderbolts"

[[1]][[2]]
[1] "Springfield"

[[1]][[3]]
[1] 102  95 110  88 103  97 105

[[1]][[4]]
[1]  98  92 105  90  99  95 102

[[1]][[5]]
[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE


[[2]]
[[2]][[1]]
[1] "Dragons"

[[2]][[2]]
[1] "Riverside"

[[2]][[3]]
[1]  88  92  85  97 103  90

[[2]][[4]]
[1]  90  88  87  95 100  92

[[2]][[5]]
[1] FALSE  TRUE  TRUE FALSE  TRUE FALSE


[[3]]
[[3]][[1]]
[1] "Eagles"

[[3]][[2]]
[1] "Lakewood"

[[3]][[3]]
[1] 105  98  92 110  99 101  95 103

[[3]][[4]]
[1] 100  95  90 108  97  99  93 101

[[3]][[5]]
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
#########################################.
# Answer - with logical values
#########################################.

# NOTE - this is confusing and is NOT the best way to answer this question.
# We will learn material later that will make it easier to answer these
# types of questions by using a logical index. For now - just try to 
# understand WHY this works.
league[c(FALSE,FALSE,FALSE,rep(TRUE,length(league)-3))]
[[1]]
[[1]][[1]]
[1] "Thunderbolts"

[[1]][[2]]
[1] "Springfield"

[[1]][[3]]
[1] 102  95 110  88 103  97 105

[[1]][[4]]
[1]  98  92 105  90  99  95 102

[[1]][[5]]
[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE


[[2]]
[[2]][[1]]
[1] "Dragons"

[[2]][[2]]
[1] "Riverside"

[[2]][[3]]
[1]  88  92  85  97 103  90

[[2]][[4]]
[1]  90  88  87  95 100  92

[[2]][[5]]
[1] FALSE  TRUE  TRUE FALSE  TRUE FALSE


[[3]]
[[3]][[1]]
[1] "Eagles"

[[3]][[2]]
[1] "Lakewood"

[[3]][[3]]
[1] 105  98  92 110  99 101  95 103

[[3]][[4]]
[1] 100  95  90 108  97  99  93 101

[[3]][[5]]
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
#----------------------------------------------------------------------------------
# QUESTION 4
# TOPIC: lists, nested lists
# 
# PART A
# The league list contains other lists for the teams. These are called
# "nested lists". Write a command that returns JUST the first and third team's lists.
# Store this new list in a variable named teams1and3.
#
# PART B
# Assuming you answered PART A correctly, what
# will the following command return - explain why.
# 
#   length(teams1and3) 
#----------------------------------------------------------------------------------
###################.
# Answer - part A
###################.

teams1and3 = league[c(4,6)]

teams1and3
[[1]]
[[1]][[1]]
[1] "Thunderbolts"

[[1]][[2]]
[1] "Springfield"

[[1]][[3]]
[1] 102  95 110  88 103  97 105

[[1]][[4]]
[1]  98  92 105  90  99  95 102

[[1]][[5]]
[1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE


[[2]]
[[2]][[1]]
[1] "Eagles"

[[2]][[2]]
[1] "Lakewood"

[[2]][[3]]
[1] 105  98  92 110  99 101  95 103

[[2]][[4]]
[1] 100  95  90 108  97  99  93 101

[[2]][[5]]
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
# Note: We use [single-bracket] which returns a list
###################.
# Answer - part B
###################.

length(teams1and3)
[1] 2
# The result is 2 because teams1and3 is a list that contains
# two lists (one for each team).
#----------------------------------------------------------------------------------
# QUESTION 5
# TOPIC: lists, creating lists
# 
# PART A
# Create a new list called team4 that represents a fourth team with 
# the following information (in this order):
#   - Team name: "Wildcats"
#   - City: "Mountainview"
#   - Points scored: 95, 101, 88, 92, 105, 99
#   - Points allowed: 93, 98, 90, 94, 102, 101
#   - Home games: TRUE, FALSE, TRUE, TRUE, FALSE, TRUE
#
# PART B
# Use the length() function to verify that team4 contains 5 items.
#
# PART C
# Use str() to examine the structure of team4.
#----------------------------------------------------------------------------------
###################.
# Answer - part A
###################.

team4 = list(
  "Wildcats",
  "Mountainview",
  c(95, 101, 88, 92, 105, 99),
  c(93, 98, 90, 94, 102, 101),
  c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)
)

team4
[[1]]
[1] "Wildcats"

[[2]]
[1] "Mountainview"

[[3]]
[1]  95 101  88  92 105  99

[[4]]
[1]  93  98  90  94 102 101

[[5]]
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE
###################.
# Answer - part B
###################.

length(team4)
[1] 5
# The result should be 5
###################.
# Answer - part C
###################.

str(team4)
List of 5
 $ : chr "Wildcats"
 $ : chr "Mountainview"
 $ : num [1:6] 95 101 88 92 105 99
 $ : num [1:6] 93 98 90 94 102 101
 $ : logi [1:6] TRUE FALSE TRUE TRUE FALSE TRUE
# This shows the structure: a list with 5 items
# - 2 character vectors (team name and city)
# - 2 numeric vectors (points scored and allowed)
# - 1 logical vector (home games)

© 2025 Y. Rosenthal. All rights reserved.