This chapter addresses the basics of R programming. When the book says “basics” it really means it.
R is probably one of the best calculators out there. It adds and multiplies really well.
1 + 4 / 2
## [1] 3
(1 + 4) / 2
## [1] 2.5
(-3)^2
## [1] 9
sqrt(10)
## [1] 3.162278
2 * pi
## [1] 6.283185
cos(2*pi)
## [1] 1
log(exp(1))
## [1] 1
Notice that R conforms to the standard order of operations. R also contains a plethora of built in mathematical functions.
We can assign values to variables. R views “=” and “<-” to be equivalent. To see what is in a variable, we can enter the variable name. While not mentioned in the text, you can make right assignments as well. This is probably why the authors prefer “<-” over “=”. As the authors note, RStudio has a keyboard shortcut of “Alt-” that embeds ” <- ” in your code.
x <- 1 + 6
x
## [1] 7
y = 2 + 7
y
## [1] 9
3 + 8 -> z
z
## [1] 11
red <- "blue"
red
## [1] "blue"
w <- "A word or two."
w
## [1] "A word or two."
a <- FALSE
a
## [1] FALSE
The text suggests using object names that contain words separated by underscores (“_”). This is fine if you intend to remain in R. However, if you ever decide to port your code to S or S-Plus, you will need to convert your underscores to periods or remove them completely. S and S-Plus use “_” as an assignment operator that is equivalent to “<-”.
R is very, Very, verY case sensitive, so it views the above names as unique.
The text suggests another RStudio keyboard shortcut for finding object names when working in the “Console.” To list all of the commands that you have typed that begin with “A” type “A Cmd/Ctrl UpArrow”. You can then move through the list with the arrow keys. Click outside of the box to return to RStudio.
R is syntactically precise — or excessively picky. Many languages are. Unfortunately, R’s error messages are often close to useless. You will need to think hard to figure out what they mean.
R has a lot of built-in functions that can make your life easier. It also allows you to define your own functions. Within RStudio is a keyboard shortcut that helps you find the function that you are looking for. For example, to find the function seq — which generates a sequence — enter into R “se” and hit “Tab” and then use the arrow keys or additional characters to get to your function. Note that the help that the book speaks of can also be obtained by entering a “?” followed by the function with which you need help. Another helpful function is args.
seq(-5, 5, by=0.5)
## [1] -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0
## [16] 2.5 3.0 3.5 4.0 4.5 5.0
### Note that this is simpler and clearer than
(-10:10)/2
## [1] -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0
## [16] 2.5 3.0 3.5 4.0 4.5 5.0
### which, while equivalent, requires some mental arithmetic
### We can learn what R expects to have passed to a function
args(abline)
## function (a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL,
## coef = NULL, untf = FALSE, ...)
## NULL
Many people are confused by the “+” that shows up unannounced when entering code into R. As the book points out, it usually means that you have dropped a closing single/double quotation mark, brace, curly brace, or parenthesis.
### To see the "+" that occurs when you drop a parenthesis, run the
### following code in the R console
seq(-1, 1, by = 0.2
### The following parenthesis closes the function call and allows knitting
### in RStudio
)
## [1] -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0
The authors finish with a comment about the possibility of showing the contents of the object to which an assignment was just made.
(n <- 7)
## [1] 7
(y <- seq(2, 4, length = 7))
## [1] 2.000000 2.333333 2.666667 3.000000 3.333333 3.666667 4.000000
ls()
## [1] "a" "n" "red" "w" "x" "y" "z"
And, yes, all of the objects that have been created during this session can be seen under the “Environment” tab in RStudio. The text fails to mention that you can remove unwanted objects by using the rm function.
Look for n and y under the “values” and then run the following code.
rm(n)
rm(y)
ls()
## [1] "a" "red" "w" "x" "z"
If you recheck “values” you will see that the objects no longer exist.