generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
01-introduction.Rmd
140 lines (93 loc) · 3.71 KB
/
01-introduction.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
# Introduction to R {#introduction}
## Getting Started
The simplest way to use R is to use it as if it were a calculator. For example, if we want to know what one plus one is, you may type:
```{r results='hide', message=FALSE, eval=FALSE}
1 + 1
```
We can use any arithmetic operator, like addition, subtraction, multiplication, divison, exponentiation, and modulus operations:
```{r results='hide', message=FALSE, eval=FALSE}
# addition
1 + 1
# subtraction
6 - 4
# multiplication
2 * 2
# division
10 / 5
10.2 / 5
# integer divison
15.2 %/% 5
15.7 %/% 5
# modulus
15.2 %% 5
15.8 %% 5
# exponential
2^3
```
R also provides numerous built-in functions to use in calculations, such as natural logs, exponentiation, square root, absolute value:
```{r results='hide', message=FALSE, eval=FALSE}
# natural log
log(10)
# exponentiation
exp(2)
# square root
sqrt(4)
# absolute value
abs(-4)
```
R includes extensive facilities for accessing documentation and searching for help. This is useful to get more information about a specific function. The `help()` function and `?` help operator in R provide access to the documentation pages for R functions. For example, to get help with the round() function, we submit the following code:
```{r results='hide', message=FALSE, eval=FALSE}
#help() function
help(round)
#? operator
?round()
```
## Variable Assignment
A basic concept in programming is called a variable. A variable allows you to store a value (e.g. 10) or an object (e.g. a function description) in R. We can then further use the variable's name to access the value or the object that is stored within this variable.
For example, you can assign a value 10 to a variable `my_variable`:
```{r results='hide', message=FALSE, eval=FALSE}
my_variable <- 10
```
To print out the value of the variable, you simply type the name of your variable:
```{r results='hide', message=FALSE, eval=FALSE}
my_variable
```
A valid variable name consists of letters, numbers and the dot or underline characters. The variable name starts with a letter or the dot not followed by a number:
```{r results='hide', message=FALSE, eval=FALSE}
# A variable can be operated on
my_variable + 1
# And passed to a function
sqrt(my_variable + 1)
# To reassign a variable, just reassign in
my_variable <- 3000
# You can also operate on and reassign a variable to itself
my_variable <- my_variable + 1
```
You can broaden assignments beyond numbers:
```{r results='hide', message=FALSE, eval=FALSE}
result <- sqrt(9)
fruit_1 <- "apple"
fruit_2 <- "banana"
fruit_3 <- "cantaloupe"
```
## Finding Variables
To know all the variables currently available in the workspace we use the `ls()` function:
```{r results='hide', message=FALSE, eval=FALSE}
print(ls())
```
The `ls()` function can also use patterns to match the variable names.
```{r results='hide', message=FALSE, eval=FALSE}
# List the variables starting with the pattern "var".
print(ls(pattern = "var"))
```
## Deleting Variables
Variables can be deleted by using the `rm()` function. Below we delete the variable my_variable:
```{r results='hide', message=FALSE, eval=FALSE}
my_variable <- 5
rm(my_variable)
```
All the variables can be deleted by using the `rm()` and `ls()` function together:
```{r results='hide', message=FALSE, eval=FALSE}
rm(list = ls())
```
Another common way to remove all variables in the R environment is to click on the little broom icon next to the button `Import Dataset` under the Environment tab. One can also go up to `Session` and do `Restart R` or `New Session` (the `Restart R` and `New session` options might come in handy for when programs crash).