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 betterstr(league)
#----------------------------------------------------------------------------------# 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.#----------------------------------------------------------------------------------
part a - click here for answer
###################.# Answer - part A###################.mode(league)
[1] "list"
# The result is "list"
part b - click here for answer
###################.# Answer - part B###################.length(league)
[1] 6
# The result is 6 (3 general info items + 3 teams)
part c - click here for answer
###################.# 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.#----------------------------------------------------------------------------------
part a - click here for answer
###################.# Answer - part A###################.league[4]
# 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
part b - click here for answer
###################.# Answer - part B###################.mode(league[4])
[1] "list"
# The result is "list"
part c - click here for answer
###################.# Answer - part C###################.league[c(4, 6)]
# 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.#----------------------------------------------------------------------------------
PART A - click here for answer
#########################################.# Answer - with positive index numbers#########################################.league[4:length(league)]
#########################################.# 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))]
#----------------------------------------------------------------------------------# 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) #----------------------------------------------------------------------------------
click here for answer
###################.# Answer - part A###################.teams1and3 = league[c(4,6)]teams1and3
# Note: We use [single-bracket] which returns a list
part c - click here for answer
###################.# 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.#----------------------------------------------------------------------------------
# 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)