Get Started

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”

View Course

Exercise instructions

  • Take a look at your portfolio. It seems that someone has added microsoft stock that you did not buy!
  • Remove the microsoft element of portfolio using NULL.

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

This exercise is part of the course

Introduction to R for Finance

BeginnerSkill Level
4.8+
12 reviews

Learn essential data structures such as lists and data frames and apply that knowledge directly to financial examples.

Wouldn't it be nice if there was a way to hold related vectors, matrices, or data frames together in R? In this final chapter, you will explore lists and many of their interesting features by building a small portfolio of stocks.

Exercise 1: What is a list?Exercise 2: Create a listExercise 3: Named listsExercise 4: Access elements in a listExercise 5: Adding to a listExercise 6: Removing from a list
Exercise 7: A few list creating functionsExercise 8: Split itExercise 9: Split-Apply-CombineExercise 10: AttributesExercise 11: Congratulations!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free