Modifying Lists
Part of the Fundamentals section of Coddy's R journey — lesson 73 of 78.
Now that you know how to create lists and access their elements, let's learn how to modify them. You can change existing elements, add new ones, or remove elements entirely.
To change an existing element, use double brackets with assignment:
my_list <- list(10, "hello", TRUE)
my_list[[2]] <- "world"
print(my_list)Output:
[[1]]
[1] 10
[[2]]
[1] "world"
[[3]]
[1] TRUETo add a new element, simply assign to an index that doesn't exist yet:
my_list <- list(10, "hello")
my_list[[3]] <- FALSE
print(my_list)Output:
[[1]]
[1] 10
[[2]]
[1] "hello"
[[3]]
[1] FALSETo remove an element, assign NULL to it:
my_list <- list(10, "hello", TRUE)
my_list[[2]] <- NULL
print(my_list)Output:
[[1]]
[1] 10
[[2]]
[1] TRUENotice that after removing the second element, the remaining elements shift to fill the gap. The element that was at position 3 is now at position 2.
Challenge
EasyYou will receive three lines of input:
- Three comma-separated values to initialize a list (e.g.,
50,hello,TRUE) - An index and a new value separated by a comma for modification (e.g.,
2,goodbye) - An index of the element to remove (e.g.,
3)
Perform the following operations:
- Create a list with the three initial values (convert them to appropriate types: numeric, character, logical)
- Print the original list
- Modify the element at the specified index with the new value (keep it as a character string)
- Print the list after modification
- Add a new fourth element to the list with the value
999 - Print the list after adding the new element
- Remove the element at the specified index (from the third input)
- Print the final list
For example, if the inputs are:
100,world,FALSE
1,updated
2The output should be:
[[1]]
[1] 100
[[2]]
[1] "world"
[[3]]
[1] FALSE
[[1]]
[1] "updated"
[[2]]
[1] "world"
[[3]]
[1] FALSE
[[1]]
[1] "updated"
[[2]]
[1] "world"
[[3]]
[1] FALSE
[[4]]
[1] 999
[[1]]
[1] "updated"
[[2]]
[1] FALSE
[[3]]
[1] 999Explanation:
- Original list: 100, "world", FALSE
- After modifying index 1: "updated", "world", FALSE
- After adding fourth element: "updated", "world", FALSE, 999
- After removing index 2: "updated", FALSE, 999 (remaining elements shift)
Remember to use double brackets [[ ]] for modifications and assign NULL to remove elements.
Cheat sheet
To modify an existing element in a list, use double brackets with assignment:
my_list <- list(10, "hello", TRUE)
my_list[[2]] <- "world"To add a new element to a list, assign to an index that doesn't exist yet:
my_list <- list(10, "hello")
my_list[[3]] <- FALSETo remove an element from a list, assign NULL to it:
my_list <- list(10, "hello", TRUE)
my_list[[2]] <- NULLWhen an element is removed, the remaining elements shift to fill the gap. The element that was at position 3 becomes position 2.
Try it yourself
# Read input
con <- file("stdin", "r")
initial_values <- suppressWarnings(readLines(con, n = 1)) # Three comma-separated values
modify_input <- suppressWarnings(readLines(con, n = 1)) # Index and new value
remove_index <- suppressWarnings(readLines(con, n = 1)) # Index to remove
close(con)
# Parse the initial values
values <- strsplit(initial_values, ",")[[1]]
val1 <- as.numeric(values[1])
val2 <- values[2]
val3 <- as.logical(values[3])
# Parse modification input
mod_parts <- strsplit(modify_input, ",")[[1]]
mod_index <- as.numeric(mod_parts[1])
mod_value <- mod_parts[2]
# Parse remove index
rem_index <- as.numeric(remove_index)
# TODO: Write your code below
# 1. Create a list with the three initial values (val1, val2, val3)
# 2. Print the original list
# 3. Modify the element at mod_index with mod_value
# 4. Print the list after modification
# 5. Add a new fourth element with value 999
# 6. Print the list after adding
# 7. Remove the element at rem_index (use NULL)
# 8. Print the final 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