The Big R-Book. Philippe J. S. De Brouwer
Чтение книги онлайн.
Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 22
![The Big R-Book - Philippe J. S. De Brouwer The Big R-Book - Philippe J. S. De Brouwer](/cover_pre848614.jpg)
Sys.getenv()
In the example above the function got three variables supplied, hence only report on these three. You will also notice that the variables are wrapped in a special function c(…)xs
. This is because the function Sys.getenv()
expects one vector as argument and the function c()
will create the vector out of a list of supplied arguments.
Note that in this paragraph above name of the function Sys.getenv()
is mono-spaced. That is our convention to use code within text. Even in the index, at the end of this book, we will follow that convention.
You will also have noticed that in text – such as this line – we refer to code fragments and functions, using fixed width font such as for example “the function mean()
calculates the average.” When this part of the code needs emphasizing or is used as a word in the sentence, we might want to highlight it additionally as follows: mean(1 + pi)
.
Some other conventions also follow from this small piece of code.We will assume that you are using Linux (unless mentioned otherwise). But do notworry: that is not something that will stand in your way. In Chapter 4 “The Basics of R” on page 21, we will get you started in Windows and all other things will be pretty much the same. Also, while most books are United States centric, we want to be as inclusive as possible and not assume that you live in the United States working from United States data.
United States of America
ISO standard
As a rule, we take a country-agnostic stance and follow the ISO standards3, for dates and dimensions of other variables. For example, we will use meters instead of feet.
Learning works best when you can do something with the knowledge that you are acquiring. Therefore, we will usually even show the code of a plot that is mainly there for illustrative purposes, so you can immediately try everything yourself.
When the code produces a plot (chart or graph), then the plot will appear generally at that point between the code lines. For example, consider we want to show the generator function for the normal distribution.
# generate 1000 random numbers between 0 and 100 x <- rnorm(1000, mean = 100, sd = 2) # to illustrate previous, we show the histogram. hist(x, col = "khaki3")# This code follows the ‘hist’ command. # In rare cases the plot will be on the this page # alone and this comment is the previous page.
In most cases, the plot will be just after the code that it generates – even if the code continues after the plot(…)
command. Therefore, the plot will usually sit exactly where the code creates it. However, in some rare cases, this will not be possible (it would create page layout that would not be aesthetically appealing). The plot will then appear near the code chunk (maybe on the next page). To help you to find and identify the plot in such case, we will usually add a numbered caption to the plot.
The R code is so ubiquitous and integrated in the text that it will appear just where it should be (though charts might move). They are integral part of the text and the comments that appear there might not be repeated in the normal text later.
There is also some other code from the command prompt and/or from SQL environments. That code appears much less, so they are numbered and appear as in Listings 3.1 and 3.2.
Listing 3.1: This is what you would see if you start R in the command line terminal. Note that the last sign is the R-prompt, inviting you to type commands. This code fragment is typical for how code that is not in the R-language has been typeset in this book.
$ R R version 3.4.4 (2018-03-15) -- "Someone to Lean On" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] >
Listing 3.2: Another example of a command line instructions: factor, calc, and pi. This example only has CLI code and does not start R.
$ factor 1492 1492: 2 2 373 $ calc 2*2*373 1492 $ pi 60 3.14159265358979323846264338327950288419716939937510582097494
Note that in these environments, we do not “comment out” the output. We promise to avoid mixing input and output, but in some cases, the output will just be there. So in general it is only possible to copy line per line the commands to see the output on the screen. Copying the whole block and pasting it in the command prompt leads to error messages, rather than the code being executed. This is unlike the R code, which can be copied as a whole, pasted in the R-command prompt and it should all work fine.
Questions or tasks look as follows:
Consider Figure 3.1 on page 13. Now, imagine that you did not generate the data, but someone gave it to you, so that you do not know how it was generated. Then what could this data represent? Or, rephrased, what could xs
be? Does it look familiar?
Figure 3.1: An example showing the histogram of data generated from the normal distribution.
Questions or tasks can be answered by the tools and methods explained previously. Note that it might require to do some research by your own, such as looking into the help files or other documentation (we will of course explain how to access these). If you are using this book to prepare for an exam or test, it is probably a good preparation, but if you are in a hurry it is possible to skip these (in this book they do not add to the material explained). However, in general thinking about the issues presented will help you to solve your data-problems more efficiently.
Note that the answers to most questions can be found in E “Answers to Selected Questions” on page 1061.The answer might not always be detailed but it should be enough to solve the issue raised.
Definition: This is a definition
This is not an book about exact mathematics. This is a pragmatic book with a focus on practical