Repository navigation
Expand file tree
/
Copy path02-language-basics.Rmd
More file actions
94 lines (70 loc) · 2.22 KB
/
Copy path02-language-basics.Rmd
File metadata and controls
94 lines (70 loc) · 2.22 KB
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
# Language basics
## Assignments, arithmetic operations
There are two types of `R` commands: expressions and assignments.
__Expression__
```{r}
cos(pi)
```
__Assignments and expressions__
```{r}
x <- 1 + 2 # or x = 1 + 2
x
y = 4
x == y
```
Using `;` we can type two commands on the same line before executing them:
```{r}
e <- exp (1); log (e)
```
Some examples of arithmetic and Boolean operators:
```{r}
3*4
12/3
2^3
sqrt (16)
1 == 2
1!= 1
FALSE & TRUE # and
FALSE | TRUE # or
```
## Mode, length and class
In R, everything is an _object_. The _mode_ specifies what an object can contain. The main modes are:
- `numeric`: real numbers
- `character`: character strings
- `logical`: logical values true / false
- `list`: list, collection of objects
- `function`: function
`numeric`,` character`, and `logical` objects are _simple_ objects that can contain data of only one type. On the contrary, `list` mobjects are special objects that can contain other objects.
You can access the mode of an object with the `mode ()` function:
```{r}
age = c(33,28, 33) # The concatenation function c() allows to create vectors
mode(age)
names <- c('Daniel', 'Jehanne', 'Romain')
mode(names)
my.list <- list(Names = names, Age = age)
mode(my.list)
mode(is.integer(pi))
mode
```
Besides the mode, an object also has a _length_, defined as the number of elements it contains:
```{r}
length(age)
length(names)
length(my.list)
```
The _class_ of an object specifies its behavior and therefore its way of interacting with operations and functions. An important example are _data frames_: special lists whose elements all have the same length. The class of a data frame is different from that of generic lists and data frames have an indexing system that does not exist for other lists:
```{r}
class(my.list)
my.data.frame = data.frame(names, age)
mode(my.data.frame)
class(my.data.frame)
my.data.frame[1,2] # to extract the 1st element from the 2nd "column"
# Try the following command:
# my.list[1,2]
```
A special object is the missing value `NA`. By default, its mode is `logical`, however` NA` is neither `TRUE` nor` FALSE`. To test if a value is missing we will use the `is.na ()` function:
```{r}
NA == NA # not what we want
is.na(NA)
is.na(mean(c(1,4, NA)))
```