-
Notifications
You must be signed in to change notification settings - Fork 2
/
18-RGlossary.Rmd
305 lines (232 loc) · 8.11 KB
/
18-RGlossary.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# R Glossary
## Arguments
Inputs supplied to a function. Arguments can be either defined by their position
in the function declaration ("positional argument"; e.g., `function (x, y, z = 0)`:
the first value supplied to the function will be given the name `x`, and the
second one the name `y`) or by a tag / name (e.g., `z` in `function (x, y, z = 0)`:
in the function call, the value will have to be explicitly linked to `z`, and if
no value is linked to `z`, `z` will default to 0).
## Block
A sequence of statements, grouped between curly braces.
##
Console pane
## CRAN
Comprehensive R Archive Network. The official repository where the versions of R
and all published packages can be downloaded from. Use `install.packages("package_name")`
to download and install a new package from CRAN.
## Environment Pane
## Evaluate
## Expression
R code consists of expressions.
There are multiple types of expressions:
- constant (that is, a character string or a number). E.g.,
```
> 123
[1] 123
```
```
> "abc"
[1] abc
```
- operator expression (that is, every expression that contains one of R's operators):
```
> 2 + 3
[1] 5
```
```
> a <- "abc"
[1]
abc>
```
```
2 > 3
[1]
FALSE
```
- index constructions (that is, extracting elements from a vector or list using
numerical or name indices):
```
> c(1, 2, 3)[-1]
[1] 2 3
```
```
> fruit <- list(apple = 5, pear = 2)
> fruit[["apple"]][1]
5
```
```
> fruit$pear[1]
2
```
- flow control element: loops, conditional expressions,...
```
> if (x %% 2 == 1) print("odd") else print("even")
```
```
> for (i in 1:10) print(i)
```
- compound expression ("block"): a series of expressions grouped between curly
braces and separated by semicolons or new lines:
```
> {x <- 1; x += 5}
```
```
> {
x <- 1
x += 5
}
```
- function definition:
```
> function(x, y) x + y
```
```
> function(x,y) {
x + y
}
```
- function call:
```
> print(5)
[1]
5
```
## File pane
## Function:
A function is a (sequence of) statements that is not evaluated immediately when
it is "declared" (that is, created/written), but only when it is "called" / "invoked"
(that is, when you tell R to evaluate it). Functions have their own environment
of variables; if you assign a value to a symbol that is already used in another
environment, the variable in that other environment will be left untouched.
When you call a function, you can pass values to that function through its list
of arguments, which is a comma-separated list between brackets following the
function's name.
In order to "declare" (create) a function, the keyword `function` is needed,
followed by the list of arguments (between parentheses) and the body of the function
(usually between curly braces). Functions are usually assigned to a variable name
(functions that are not linked to a name are called anonymous functions).
Functions in R are objects, which mean they can be passed to other functions as
arguments, placed in lists, etc.
## Global variable:
A variable defined outside the scope of a function.
## Import:
see Load.
## Load:
Bring data or packages from the computer's (passive) storage into its (active)
memory so they can be used in the current R session. Loading a package adds the
names of functions and other objects from that package to R's namespace.
Use library("package_name") to import/load a package into R for the current
session (in a script, group all these imports at the top of a script).
## Local variable
A variable defined inside a function. Outside that function, the variable's name
will not be bound to the value it was bound to within the function's scope.
## Package:
A collection of functions and datasets developed by a user to extend R.
Packages can be published on `CRAN`, or distributed on GitHub or other repositories.
If you want to use a package, it must first be installed and then be loaded into
your current R session. Installation must be done only once, loading every time
you start a new R session.
* Use `install.packages("package_name")` to download and install a new package from
`CRAN`.
* Use `library("package_name")` to import/load an installed package into R for the
current session (in a script, group all imports at the top of a script).
* Use `installed.packages()` to get a list of all packages installed for your R version.
## R:
A computer language developed for statistical analysis and graphics.
## RStudio:
An Integrated Development Environment (IDE) for writing, executing and debugging
R code. R comes with its own IDE but most users prefer RStudio for its user-friendliness.
## Pane:
The Rstudio window is divided into four quadrants called panes: the Source pane,
the Console pane, the Environment Pane and the File pane. The first two are for
writing code, the latter two contain a number of tabs with useful resources.
You can minimise and maximise the size of each pane by using the icons in the top
right of every pane.
To switch the order of the panes, use RStudio's "Pane layout" dialog, which you
can find in the `Options` dialog (`Tools > Options`; `RStudio > Preferences` on Mac).
## Prompt:
## Run:
Execute R code.
## Scope:
Symbols in R are bound to a specific value only within a specific environment or
"scope". E.g., the symbol of a variable defined within a function will be bound
to that variable's value only within that function (such a variable is called a
*local* variable); outside of the function, it will be considered *unbound*
(that is, not connected to a value):
```
> f <- function (x) {
doubled = x * 2
}
> doubled
Error: Object 'doubled' not found.
```
Variables declared outside a function are considered *global* variables.
If during evaluation of a function a symbol is encountered that is not in the
local environment (that is, a symbol that was not in that function's list of
arguments and that was not defined inside the function), R will search for this
symbol in the environment from which the function was called, and so on until
the global environment is reached.
## Session:
## Source pane:
## Symbol:
The name component in a variable: a variable is a value assigned to a symbol.
E.g.,
```
x <- 3
```
(x is the symbol/name of the variable, 3 its value).
## Variable:
A symbol (name) linked with a value that this symbol represents.
Linking a symbol with a value is called assigning; this is done using an
assignment operator (`<-`, `->`, `=`).
## Vector:
A vector is the main data structure in R: vectors are collections of data.
Usually, the term vector is used as shorthand for a specific type of vector in R,
so-called **atomic vectors**; they are called like that because every element
in an atomic vector is of the same data type.
There are 5 "modes" of atomic vectors, based on the data type of its elements;
only the first three are directly relevant for us:
* character vector: all elements are text strings (data type: `character`).
```
> v <- c("a", "100", "ألف", "vector elements can be very long strings")
> typeof(v)
[1] "character"
> mode(v)
[1] "character"
```
* numeric vector: all elements are of the `integer` type (whole numbers, both
positive and negative: 1, 2, -137, ...), or of the `double` type
("double precision floating point numbers": 1.2345, -125.8, pi, ...)
E.g.,
```
> v <- c(1, -300, 18.5, pi)
> v
[1] 1.000000 300.000000 18.500000 3.141593
> typeof(v)
[1] "double"
> mode(v)
[1] "numeric"
```
* logical vector: all elements are one of the boolean values TRUE or FALSE:
```
> v <- c(TRUE, TRUE, FALSE, TRUE)
> typeof(v)
[1] "logical"
> mode(v)
[1] "logical"
```
* complex vector: all elements are complex numbers (numbers that have a real and
an imaginary part)
* raw vector: all elements are raw byte objects
The `c()` function is often used to create vectors with multiple elements. But
even if you assign a single string or number to a variable, the variable will be
a vector:
```
> a <- "This is a string"
> class(a)
[1] "character" # a is a character vector!
> a[1]
[1] "This is a string" # our string is the first (and only) element of that vector!
```
Vectors have no dimensions (vs. for example tables, which have 2 dimensions:
rows and columns).