
- 286 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
eBook - ePub
Advanced R Solutions
About this book
This book offers solutions to all 284 exercises in Advanced R, Second Edition. All the solutions have been carefully documented and made to be as clear and accessible as possible. Working through the exercises and their solutions will give you a deeper understanding of a variety of programming challenges, many of which are relevant to everyday work. This will expand your set of tools on a technical and conceptual level. You will be able to transfer many of the specific programming schemes directly and will discover far more elegant solutions to everyday problems.
Features:
- When R creates copies, and how it affects memory usage and code performance
- Everything you could ever want to know about functions
- The differences between calling and exiting handlers
- How to employ functional programming to solve modular tasks
- The motivation, mechanics, usage, and limitations of R's highly pragmatic S3 OO system
- The R6 OO system, which is more like OO programming in other languages
- The rules that R uses to parse and evaluate expressions
- How to use metaprogramming to generate HTML or LaTeX with elegant R code
- How to identify and resolve performance bottlenecks
Tools to learn more effectively

Saving Books

Keyword Search

Annotating Text

Listen to it instead
Information
Part I
Foundations
2
Names and values
DOI: 10.1201/9781003175414-2
Prerequisites
In this chapter we will use the lobstr package (Wickham, 2019a) to help answer questions regarding the internal representation of R objects.

2.2 Binding basics
Q1: Explain the relationship between a, b, c, and d in the following code:

A: a, b, and c point to the same object (with the same address in memory). This object has the value 1:10. d points to a different object with the same value.

Q2: The following code accesses the mean function in multiple ways. Do they all point to the same underlying function object? Verify this with lobstr::obj_addr().

A: Yes, they point to the same object. We confirm this by inspecting the address of the underlying function object.

Q3: By default, base R data import functions, like read.csv(), will automatically convert non-syntactic names to syntactic ones. Why might this be problematic? What option allows you to suppress this behaviour?
A: Column names are often data, and the underlying make.names() transformation is non-invertible, so the default behaviour corrupts data. To avoid this, set check.names = FALSE.
Q4: What rules does make.names() use to convert non-syntactic names into syntactic ones?
A: A valid name must start with a letter or a dot (not followed by a number) and may further contain numbers and underscores ("_"s are allowed since R version 1.9.0).
Three main mechanisms ensure syntactically valid names (see ?make.names):
- Names that do not start with a letter or a dot will be prepended with an "X".
The same holds for names that begin with a dot followed by a number.
- Additionally, non-valid characters are replaced by a dot.

- Reserved R keywords (see ?reserved) are suffixed by a dot.

Interestingly, some of these transformations are influenced by the current locale. From ?make.names:
The definition of a letter depends on the current locale, but only ASCII digits are considered to be digits.
Q5: I slightly simplified the rules that govern syntactic names. Why is .123e1 not a syntactic name? Read ?make.names for the full details.
A: .123e1 is not a syntactic name, because it starts with one dot which is followed by a number. This makes it a double, 1.23.
2.3 Copy-on-modify
Q1: Why is tracemem(1:10) not useful?
A: When 1:10 is called an object with an address in memory is created, but it is not bound to a name. Therefore, the object cannot be called or manipulated from R. As no copies will be made, it is not useful to track the object for copying.

Q2: Explain why tracemem() shows two copies when you run this code. Hint: carefully look at the difference between this code and the code shown earlier in the section.

A: Initially the vector x has integer type. The replacement call assigns a double to the third element of x, which triggers copy-on-modify.

We can avoid the copy by sub-assigning an integer instead of a double:

Please be aware that running this code in RStudio will result in additional copies because of the reference from the environment pane.
Q3: Sketch out the relationship between the following objects:

A: a contains a reference to an address with the value 1:10. b contains a list of two references to the same address as a. c contains a list of b (containing two references to a), a (containing the same reference again) and a reference pointing to a different address containing the same value (1:10).

We can confirm these relationships by inspecting the reference tree in R.

Q4: What happens when you run this code:

Draw a picture.
A: The initial reference tree of x shows that the name x binds to a list ob...
Table of contents
- Cover
- Half Title
- Series Page
- Title Page
- Copyright Page
- Dedication
- Contents
- Preface
- I Foundations
- II Functional programming
- III Object-oriented programming
- IV Metaprogramming
- V Techniques
- Bibliography
Frequently asked questions
Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
- Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
- Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Advanced R Solutions by Malte Grosser,Henning Bumann,Hadley Wickham in PDF and/or ePUB format, as well as other popular books in Economics & Statistics for Business & Economics. We have over one million books available in our catalogue for you to explore.