Great! Let's move on and learn how to change, add, and delete list members.
After speaking with Emma on the phone, you discovered that you have the wrong info about her age in your list. It says she's 35
, but she mentioned that she's actually 31
. You'd like to update this information.
We learned that we can access the member named age
by using the following notation: candidate["age"]
. When you run this code, R prints 35
to the console.
To change the value of this member, we simply use the assignment operator to feed the member a new value, like this:
candidate[["age"]] <- 31
With this notation, we're able to modify the value stored in the age member of the candidate
list. Now, that member contains the value 31
.
You may use the dollar notation as well, like so:
candidate$age <- 31
The result will be exactly the same: age will now contain the value 31
.
For unnamed lists, you can use the [[]]
operator with the appropriate index. Here's an example:
candidate[[2]] <- 31