-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1A-basicOperations.qmd
More file actions
209 lines (149 loc) · 5.45 KB
/
Copy path1A-basicOperations.qmd
File metadata and controls
209 lines (149 loc) · 5.45 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
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
---
output: html_document
editor_options:
chunk_output_type: console
---
# Basic data operations with R
## Data manipulation in R
R works like a calculator:
```{r}
2+2
5*4
2^2
```
Always pay attention to the parenthese to set the order of the calculations:
```{r}
2*4^3-1
2*4^(3-1)
(2*4)^3-1
(2*4)^(3-1)
```
We can also use **functions** that perform specific calculations:
```{r}
sqrt(4)
sum(c(2,2))
abs(-1)
```
We can assign values/data to **objects**:
```{r}
object.name <- 1
variable1 <- c(2,2) # c() function to concatenate/group values
```
Note that both operators '\<-' or "=" work when creating a object.
Functions in R (e.g. `sum()`, `mean()`, etc.) have arguments that control/change their behavior and are also used to pass the data to the function:
```{r}
mean(x = c(2, 2))
mean(variable1)
```
::: {.callout-tip appearance="default"}
## Help
A list and description of all arguments in a function can be found in the help of a function (which can be accessed via `?mean` or `help(mean)`, or if you place the cursor on the function and press F1 in Rstudio).
:::
### Data types and data structures
There are four important data types in R (there are more but we focus on these 5):
- **Numeric:** 1, 2, 3, 4
- **Logical**: TRUE or FALSE
- **Characters**: "A", "B",...
- **Factors**: "A", "B",... which are characters but we have to tell R explicitly that they are factors - have levels
- Not Available/Not a Number: **NA, NaN** (empty value)
Based on the data types we can build data structures which contain either only specific data types or a mixture of data types:
- **Vector**: Several values of **one** data type, can be created with the `c` function:
```{r}
num.vec <- c(5, 3, 5, 6) # numeric vector
log.vec <- c(TRUE, TRUE, FALSE, TRUE) # logical vector
char.vec <- c("A", "B", "C") # character vector
factor.vec <- as.factor(c("A", "B", "C")) # factor vector
```
- **Matrix**: two-dimensional data structure of **one** data type, can be created with the `matrix` function (we can pass a vector to the matrix function and tell it via arguments how the matrix should be constructed):
```{r}
mat1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)
```
- **Data.frame**: Often our data has variables of different types which makes a matrix unsuitable data structure. Data.frames can handle different data types and is organized in columns (one column = one variables) and can be created with the `data.frame` function:
```{r}
data1 <- data.frame(A = c(1, 2, 3), B = c("A", "B", "C"),
C = c(TRUE, FALSE, FALSE))
```
- **List**: A list is a data structure that can contain elements of different types and different lengths, like a bag where you can include anything. It is created with the `list` function:
```{r}
my.list <- list(A = c(1, 2, 3), # a vector
B = data1, # a data.frame
C = mat1) # a matrix
```
### Data manipulation
A vector is a one dimensional data structure and we can access the values by using `[ ]`:
```{r}
vec = c(1, 2, 3, 4, 5)
vec[1] # access first element
vec[5] # access last element
```
A data.frame is a two dimensional data structure. Let's define a data.frame from two vectors:
```{r}
df = data.frame(
x = c(2, 2, 2, 3, 2, 2, 1), # add column named x
y = c(4, 5, 5, 4, 5, 3, 5) # add a second column named y
)
#Let's see how this looks like:
df
```
Access parts of the data.frame:
```{r}
df[1,2] # get element in row 1, column 1
df[7,1] # get element in row 7, column 1
df[2,] # get row 2
df[,2] # get column 2
# or use the $ sign to access columns:
df$y
df[2:4,1:2] # get rows 2 to 4 and only columns 1 and 2
```
We can also set filters:
```{r}
df[df$x > 2, ] # show only data where x is larger than 2
df[df$y == 5, ] #show only data where y equals 5
df[df$y == 5 & df$x == 1, ] #show only data where y equals 5 AND x equals 1
df[df$y == 5 | df$x == 3, ] #show data where y equals 5 OR x equals 3
```
::: {.callout-tip appearance="default" collapse="true"}
### Logical operators
| Operators | Meaning |
|-----------|-----------------------|
| \< | Lower than |
| \<= | Lower than or equal to |
| \> | Higher than |
| \>= | Higher than or equal to |
| == | Equal to |
| != | Not equal to |
| !a | Not a |
| a\|b | a or b |
| a & b | a and b |
| isTRUE(a) | Test if a is true |
: Logical operators in R
:::
Add an additional column with NA values:
```{r}
df$NAs = NA # fills up a new column named NAs with all NA values
df
```
## Data analysis workflow
This is a simple version of what you're going to learn during this course:
1. Let's say we measured the size of individuals in two different treatment groups
```{r}
group1 = c(2, 2, 2, 3, 2, 2, 1.1)
group2 = c(4, 5, 5, 4, 5, 3, 5.1)
class(group2)
```
2. Descriptive statistics and visualization
```{r}
mean(group1)
mean(group2)
boxplot(group1, group2)
```
3. Testing for differences. Question: Is there a difference between group1 and group2?
```{r}
t.test(group1, group2)
```
4. Interpretation of the results. Individuals in Group 2 were larger than those in group 1 (t test, t = -6.62, p \< 0.0001)
In the course we will work a lot with datasets implemented in R or in R packages which can be accessed via their name:
```{r}
dat = airquality
head(dat)
```