-
Notifications
You must be signed in to change notification settings - Fork 51
/
020_objects.Rmd
executable file
·122 lines (94 loc) · 5.56 KB
/
020_objects.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<style>@import url(style.css);</style>
[Introduction to Data Analysis](index.html "Course index")
# 2. Objects
[wolfram]: https://www.wolframalpha.com/
One of the first things that you want to learn in R is to manipulate objects. In R, an object can be many things: a number, a piece of text, a function, a series of values, a dataset… Let's start with the very fundamentals.
## Manipulating scalars
Let's start with numbers, as you certainly expect R to work like a scientific calculator or like [Wolfram Alpha][wolfram]. The following examples show some mathematical expressions in R. The results they return are numeric in nature and are sometimes called scalars because they fit on a scale, the real number line $\mathbb{R}$.
```{r calc, results = 'hide'}
# Addition.
1 + 2
# Brackets.
(1 + 2) / 3
# Powers.
7^2
# Infinity.
1 - Inf
# And so on. These objects are...
class(7^2)
```
These commands have created objects of a particular class: numeric objects. Train yourself by computing, for instance, $3^3 - 4$ and $e^2$. If you need help to find out how to code an exponential in R, just look for help with `??exponential`. Note that positive infinity is a number, but try dividing infinity by infinity and the class of the result will be `NaN` (Not a Number).
## Evaluating logical statements
From mathematics, you also know about logical statements, which can only be true or not-true (false). You can ask R to evaluate a given expression by submitting it as a logical expression, and R will respond with a Boolean statement, either `TRUE` or `FALSE`. The next examples will create objects of another class: logical objects.
```{r logical, results = 'hide'}
# A simple logical test with the natural logarithm.
log(1) > 0
# Guess why this result differs from the one above.
log(1) >= 0
# Equality requires TWO equal signs.
log(1) == 0
# Negation requires the "!" symbol.
log(1) != 1
# A more complex example using scientific notation.
1 + log10(1e7) == 8 * exp(1)^0
# And so on. These objects are...
class(1 > 1)
```
## Joining text strings
You might also remember manipulating text, or "strings", from our first session. There are several ways to have R "paste" and concatenate strings together, which is useful as soon as you are manipulating data like names or dates. Here are some examples:
```{r character}
# The typical "hello world" text.
"Hello R World!"
# Now "pasted" from its elements.
paste("Hello", "R", "World", "!")
# Now through raw concatenation.
cat("Hello", "R", "World", "!\n")
# And so on. These objects are...
class("some text")
```
## Manipulating vectors
Finally, R can handle multidimensional objects that hold more than one value. In fact, even single results are handled as such: in R, the scalar `r 2` is a one-dimensional numeric vector. The example below defines a simple sequence of integer values $(1, 2, 3)$, computes its sum and product, and then compares them with each other:
```{r calc-vector, results='hide'}
# Create a sequence of integers.
1:3
# Compute the sum of the sequence.
sum(1:3)
# Compute the product of the sequence.
prod(1:3)
# Compare the sum and product.
sum(1:3) == prod(1:3)
```
The `r 1:3` sequence used above is called a numeric vector. It shows up in your Workspace, along with an indication that it contains integer values and has a dimension of 3. The rest of this session will show you how to manipulate such objects.
## Fixing R syntax
Before we go further into manipulating objects, here's a quick tour of R syntax. You will inevitably make mistakes when writing R commands, which is fine -- nothing (bad) happens when you do. To correct a mistake, simply press `UpArrow` (↑) to go back into your last console input and fix it.
```{r broken-hello-world, eval=FALSE, tidy=FALSE}
# This will break. Try it.
print(Hello World!)
# Press UpArrow, select text, add double quotes.
# This will now run fine.
print("Hello World")
```
R syntax can be a bit strange: for example, for some (probably good) reason, the `install.packages` command takes quotes, whereas the `library` command does not. Quotes can be single or double and are sometimes optional.
```{r quotes-strangeness, eval=FALSE}
# This needs quotes.
install.packages("ggplot2")
# This does not.
library(ggplot2)
# But it can.
library("ggplot2")
# Here's a less noisy way to load packages.
require(ggplot2)
# Thankfully, it also accepts quotes.
require("ggplot2")
# Oh, and single quotes will work too.
require('ggplot2')
```
Note that brackets are omnipresent in R syntax: every function (or command) will require them. The bits written inside the brackets and separated by commas are called arguments. The `paste0()` function, for instance, accepts any number of arguments. It concatenates (joins) elements into a single character vector, without inserting spaces between them like the `paste()` function does.
```{r cat}
# Print some text, the full date and some text again, with no separators.
paste0("Hello World! Today is ", date(), ".")
```
The syntax of each command requires a bit of memorization, and lots of practice. Nobody types functional code in R without going through cycles of trial-and-error with it. Programming requires some discipline, some logic and a lot of patience.
A final thing: it is good practice to write code in a certain style. For example, R is insensitive to extra blank space, but it is good practice to use blank space only when it helps to align elements. The [Google R Style Guide](https://google.github.io/styleguide/Rguide.xml) is a good starting point to learn a few common conventions.
Let's now turn to manipulating simple things in R.
> __Next__: [Vectors and matrices](021_vectors.html).