Loop over a list
Looping over a list is just as easy and convenient as looping over a vector. There are again two different approaches here:
primes_list <- list(2, 3, 5, 7, 11, 13)
# loop version 1
for (p in primes_list) {
print(p)
}
# loop version 2
for (i in 1:length(primes_list)) {
print(primes_list[[i]])
}
Notice that you need double square brackets - [[ ]]
- to select the list elements in loop version 2.
Suppose you have a list of all sorts of information on New York City: its population size, the names of the boroughs, and whether it is the capital of the United States. We've already defined a list nyc
containing this information (source: Wikipedia).
This is a part of the course
“Intermediate R”
Exercise instructions
As in the previous exercise, loop over the nyc
list in two different ways to print its elements:
- Loop directly over the
nyc
list (loop version 1). - Define a looping index and do subsetting using double brackets (loop version 2).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The nyc list is already specified
nyc <- list(pop = 8405837,
boroughs = c("Manhattan", "Bronx", "Brooklyn", "Queens", "Staten Island"),
capital = FALSE)
# Loop version 1
# Loop version 2
This exercise is part of the course
Intermediate R
Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.
Chapter 1: Conditionals and Control Flow
In this chapter, you'll learn about relational operators for comparing R objects, and logical operators like "and" and "or" for combining TRUE and FALSE values. Then, you'll use this knowledge to build conditional statements.
Exercise 1: Relational OperatorsExercise 2: EqualityExercise 3: Greater and less thanExercise 4: Compare vectorsExercise 5: Compare matricesExercise 6: Logical OperatorsExercise 7: & and |Exercise 8: & and | (2)Exercise 9: Reverse the result: !Exercise 10: Blend it all togetherExercise 11: Conditional StatementsExercise 12: The if statementExercise 13: Add an elseExercise 14: Customize further: else ifExercise 15: Else if 2.0Exercise 16: Take control!Chapter 2: Loops
Loops can come in handy on numerous occasions. While loops are like repeated if statements, the for loop is designed to iterate over all elements in a sequence. Learn about them in this chapter.
Exercise 1: While loopExercise 2: Write a while loopExercise 3: Throw in more conditionalsExercise 4: Stop the while loop: breakExercise 5: Build a while loop from scratchExercise 6: For loopExercise 7: Loop over a vectorExercise 8: Loop over a listExercise 9: Loop over a matrixExercise 10: Mix it up with control flowExercise 11: Next, you break itExercise 12: Build a for loop from scratchChapter 3: Functions
Functions are an extremely important concept in almost every programming language, and R is no different. Learn what functions are and how to use them—then take charge by writing your own functions.
Exercise 1: Introduction to FunctionsExercise 2: Function documentationExercise 3: Use a functionExercise 4: Use a function (2)Exercise 5: Use a function (3)Exercise 6: Functions inside functionsExercise 7: Required, or optional?Exercise 8: Writing FunctionsExercise 9: Write your own functionExercise 10: Write your own function (2)Exercise 11: Write your own function (3)Exercise 12: Function scopingExercise 13: R passes arguments by valueExercise 14: R you functional?Exercise 15: R you functional? (2)Exercise 16: R PackagesExercise 17: Load an R PackageExercise 18: Different ways to load a packageChapter 4: The apply family
Whenever you're using a for loop, you may want to revise your code to see whether you can use the lapply function instead. Learn all about this intuitive way of applying a function over a list or a vector, and how to use its variants, sapply and vapply.
Exercise 1: lapplyExercise 2: Use lapply with a built-in R functionExercise 3: Use lapply with your own functionExercise 4: lapply and anonymous functionsExercise 5: Use lapply with additional argumentsExercise 6: Apply functions that return NULLExercise 7: sapplyExercise 8: How to use sapplyExercise 9: sapply with your own functionExercise 10: sapply with function returning vectorExercise 11: sapply can't simplify, now what?Exercise 12: sapply with functions that return NULLExercise 13: Reverse engineering sapplyExercise 14: vapplyExercise 15: Use vapplyExercise 16: Use vapply (2)Exercise 17: From sapply to vapplyChapter 5: Utilities
Mastering R programming is not only about understanding its programming concepts. Having a solid understanding of a wide range of R functions is also important. This chapter introduces you to many useful functions for data structure manipulation, regular expressions, and working with times and dates.
Exercise 1: Useful FunctionsExercise 2: Mathematical utilitiesExercise 3: Find the errorExercise 4: Data UtilitiesExercise 5: Find the error (2)Exercise 6: Beat Gauss using RExercise 7: Regular ExpressionsExercise 8: grepl & grepExercise 9: grepl & grep (2)Exercise 10: sub & gsubExercise 11: sub & gsub (2)Exercise 12: Times & DatesExercise 13: Right here, right nowExercise 14: Create and format datesExercise 15: Create and format timesExercise 16: Calculations with DatesExercise 17: Calculations with TimesExercise 18: Time is of the essenceWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.