Advanced R Solutions
eBook - ePub

Advanced R Solutions

Malte Grosser, Henning Bumann, Hadley Wickham

  1. 286 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Advanced R Solutions

Malte Grosser, Henning Bumann, Hadley Wickham

Book details
Book preview
Table of contents
Citations

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

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
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 here.
Is Advanced R Solutions an online PDF/ePUB?
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.

Information

Year
2021
ISBN
9781000409093
Edition
1

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):
  1. 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.
  2. Additionally, non-valid characters are replaced by a dot.
  3. 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

Citation styles for Advanced R Solutions

APA 6 Citation

Grosser, M., Bumann, H., & Wickham, H. (2021). Advanced R Solutions (1st ed.). CRC Press. Retrieved from https://www.perlego.com/book/2825825/advanced-r-solutions-pdf (Original work published 2021)

Chicago Citation

Grosser, Malte, Henning Bumann, and Hadley Wickham. (2021) 2021. Advanced R Solutions. 1st ed. CRC Press. https://www.perlego.com/book/2825825/advanced-r-solutions-pdf.

Harvard Citation

Grosser, M., Bumann, H. and Wickham, H. (2021) Advanced R Solutions. 1st edn. CRC Press. Available at: https://www.perlego.com/book/2825825/advanced-r-solutions-pdf (Accessed: 15 October 2022).

MLA 7 Citation

Grosser, Malte, Henning Bumann, and Hadley Wickham. Advanced R Solutions. 1st ed. CRC Press, 2021. Web. 15 Oct. 2022.