Named Lists
Part of the Fundamentals section of Coddy's R journey — lesson 74 of 78.
Just like named vectors, lists can have names assigned to their elements. This makes your code more readable and allows you to access elements by meaningful labels instead of numeric positions.
You can assign names when creating the list:
person <- list(name = "Alice", age = 25, active = TRUE)
print(person)Output:
$name
[1] "Alice"
$age
[1] 25
$active
[1] TRUENotice how the output now shows $name instead of [[1]]. To access named elements, use the $ operator:
person <- list(name = "Alice", age = 25, active = TRUE)
print(person$age)Output:
[1] 25You can also use double brackets with the name as a string, which is useful when the name is stored in a variable:
person <- list(name = "Alice", age = 25)
print(person[["name"]])Output:
[1] "Alice"Modifying named elements works the same way. Use $ to change existing values or add new ones:
person <- list(name = "Alice", age = 25)
person$age <- 26
person$city <- "Paris"
print(person)Output:
$name
[1] "Alice"
$age
[1] 26
$city
[1] "Paris"Challenge
EasyYou will receive three lines of input:
- A book title (e.g.,
The Great Gatsby) - A book author (e.g.,
F. Scott Fitzgerald) - The year published (e.g.,
1925)
Perform the following operations:
- Create a named list called
bookwith three elements:title,author, andyear(convert year to numeric) - Print the book's title using the
$operator - Print the book's year using double brackets with the name as a string
- Update the
yearto be 1 year later using the$operator - Add a new element called
availablewith the valueTRUE - Print the complete updated list
For example, if the inputs are:
1984
George Orwell
1949The output should be:
[1] "1984"
[1] 1949
$title
[1] "1984"
$author
[1] "George Orwell"
$year
[1] 1950
$available
[1] TRUEExplanation:
book$titleprints the title valuebook[["year"]]prints the original year- The year is updated from 1949 to 1950
- A new
availableelement is added with valueTRUE
Cheat sheet
Lists can have names assigned to their elements for better readability and access by meaningful labels.
Create a named list by assigning names during creation:
person <- list(name = "Alice", age = 25, active = TRUE)Access named elements using the $ operator:
person$age # Returns 25Access named elements using double brackets with the name as a string:
person[["name"]] # Returns "Alice"Modify existing named elements:
person$age <- 26Add new named elements:
person$city <- "Paris"Try it yourself
# Read input
con <- file("stdin", "r")
title <- suppressWarnings(readLines(con, n = 1))
author <- suppressWarnings(readLines(con, n = 1))
year <- suppressWarnings(readLines(con, n = 1))
close(con)
# TODO: Write your code below
# 1. Create a named list called 'book' with elements: title, author, and year (convert year to numeric)
# 2. Print the book's title using the $ operator
# 3. Print the book's year using double brackets with the name as a string
# 4. Update the year to be 1 year later using the $ operator
# 5. Add a new element called 'available' with value TRUE
# 6. Print the complete updated listThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values