8 Doing things conditionally: Control structures
This chapter covers
- Looping over sections of code
- Conditionally evaluating code
- Best practices to balance speed and readability
You saw in chapter 6 that you can conditionally select elements from data structures, but sometimes you only want to do (or not do) something based on some condition, or do something many times with slightly different conditions. There may be many steps you want to conditionally perform, so you need a way to only do things sometimes (or many times). You can use several approaches to do this in R, so let’s look at the most common and the most useful, as well as some other ways these tasks can be accomplished.
8.1 Looping
So far, you’ve evaluated code in a very linear ...
Get Beyond Spreadsheets with R now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.