From objects to dataframes
name <- value: <- creates objects
my_data, x1 (not 1x, my data)Name and name are two different objectsrm(x) removes one object, rm(list = ls()) removes all| operator | meaning | example | result |
|---|---|---|---|
+ - * / |
arithmetic | 6 * 2 / 4 |
3 |
^ |
power | 2 ^ 3 |
8 |
%% |
remainder of the division | 9 %% 2 |
1 |
TRUE counts as 1, FALSE as 0: sum() counts, mean() gives the proportion
One dimension, all elements of the same type
is. and as.Mixing types converts everything to one type:
Operations work element by element, summary functions return one value
which()Indexing + <- modifies only the selected elements:
[1] caffeine nicotine caffeine
Levels: caffeine nicotine placebo
To use a new value, first add it to the levels
[1] placebo caffeine nicotine placebo caffeine
Levels: caffeine nicotine placebo
factor() with both arguments:
levels: the existing values, in the order we wantlabels: their new names, in the same orderFor ordinal categories (< and > make sense), set the order and ordered = TRUE
[ ], [[ ]] and $[row, column]Each column is a vector (all the same length): a list with a rectangular shape
'data.frame': 5 obs. of 4 variables:
$ id : chr "P1" "P2" "P3" "P4" ...
$ group: chr "ctrl" "trt" "ctrl" "trt" ...
$ age : num 22 27 31 NA 24
$ score: num 18 25 30 22 27
summary(df) and table(df$group) are also useful checks
df[rows, columns]: leave a position empty to keep everything
df[rows, columns]: the condition goes in the rows position
subset()Same result, without repeating df$:
df$new_column <- value: one value for each row
ifelse()A new column that depends on a condition
'data.frame': 5 obs. of 7 variables:
$ id : chr "P1" "P2" "P3" "P4" ...
$ group : Factor w/ 2 levels "ctrl","trt": 1 2 1 2 2
$ age : num 22 27 31 NA 24
$ score : num 18 25 30 22 27
$ passed : logi FALSE TRUE TRUE FALSE TRUE
$ score_100: num 60 83.3 100 73.3 90
$ level : chr "low" "high" "high" "low" ...
Select the cell with a condition, then assign the new value
Assign NULL to the column
order()order() gives the positions of the rows, from the lowest to the highest value
decreasing = TRUE
rbind()The new rows need the same number of columns and the same column names
id group age score passed level
1 P1 ctrl 22 18 FALSE low
2 P2 trt NA 25 TRUE high
3 P3 ctrl 31 30 TRUE high
4 P4 trt 29 22 FALSE low
5 P5 trt 24 27 TRUE high
6 P6 ctrl 25 21 FALSE low
merge()Add the columns of another dataframe, matching the rows by a key (here id)
merge(): the resultEach row of df2 gets the hours of its own id, even if the order is different
merge() when some ids are missingHere sleep2 has only some of the ids of df2, and one more (P7)
id hours
1 P1 7
2 P2 6
3 P7 8
all.x = TRUEThe rows of df2 without a match get NA
<- creates objects, == compares"hello", "5"mean(x, na.rm = TRUE)levels(x); to reorder and rename use factor(x, levels, labels)[[ ]] or $ to get an elementdf[rows, columns], e.g. df[df$score > 20, ]rbind(): same column names; merge(): a common key| structure | dimensions | types | create | index |
|---|---|---|---|---|
| vector | 1 | one | c(), seq(), rep() |
x[i] |
| factor | 1 | categories | factor() |
x[i], levels() |
| list | 1 (nested) | any | list() |
x[i], x[[i]], x$name |
| matrix | 2 | one | matrix(), cbind() |
x[row, col] |
| dataframe | 2 | one per column | data.frame() |
x$col, x[row, col] |
During the exam you may use the material provided during the lessons.
Other online material (or ChatGPT) is not allowed.