Removing from a list
The natural next step is to learn how to remove elements from a list. You decide that even though Dan is your best friend, you don't want his info in your list. To remove dans_movie
:
my_list$dans_movie <- NULL
my_list
$my_words
[1] "I <3 R"
$my_numbers
[1] 42 24
Using NULL
is the easiest way to remove an element from your list! If your list is not named, you can also remove elements by position using my_list[1] <- NULL
or my_list[[1]] <- NULL
.
This is a part of the course
“Introduction to R for Finance”
Exercise instructions
- Take a look at your
portfolio
. It seems that someone has addedmicrosoft
stock that you did not buy! - Remove the
microsoft
element ofportfolio
usingNULL
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Take a look at portfolio
# Remove the microsoft stock prices from your portfolio