Day 3
Freie Universität Berlin @ Theoretical Ecology
Note
Vectors always include values from the same data type (i.e., numeric or categorical)
by)seq(from = 2, to = 4, by = 0.5)[1] 2.0 2.5 3.0 3.5 4.0
seq(2, 4, by = 0.5)[1] 2.0 2.5 3.0 3.5 4.0
by is calculated from length
seq(2, 4, length = 9)[1] 2.00 2.25 2.50 2.75 3.00 3.25 3.50 3.75 4.00
by = 1
-1 works5:-5 [1] 5 4 3 2 1 0 -1 -2 -3 -4 -5
Helpful for observational or experimental data with replicates
Replicate a complete vector
rep(1:4, times = 3) [1] 1 2 3 4 1 2 3 4 1 2 3 4
rep(1:4, each = 3) [1] 1 1 1 2 2 2 3 3 3 4 4 4
tibble() herebase::data.frame with some improvementstibble(x = 1:10, treatment = "control")# A tibble: 10 × 2
x treatment
<int> <chr>
1 1 control
2 2 control
3 3 control
4 4 control
5 5 control
6 6 control
7 7 control
8 8 control
9 9 control
10 10 control
tibble()
# A tibble: 6 × 2
temp treatment
<int> <chr>
1 2 control
2 3 control
3 4 control
4 2 CO2
5 3 CO2
6 4 CO2
dplyr::mutate()
dat1$tree_species <- "pine"
dat1# A tibble: 4 × 3
temp treatment tree_species
<int> <chr> <chr>
1 2 control pine
2 3 control pine
3 2 CO2 pine
4 3 CO2 pine
bind_rows(dat_pine, dat_beech, dat_oak)# A tibble: 12 × 3
temp treatment tree_species
<dbl> <chr> <chr>
1 2 control pine
2 3 control pine
3 2 CO2 pine
4 3 CO2 pine
5 2 control beech
6 3 control beech
7 2 CO2 beech
8 3 CO2 beech
9 2 control oak
10 3 control oak
11 2 CO2 oak
12 3 CO2 oak
bind_rows(dat_pine, dat_beech)# A tibble: 8 × 4
temp treatment Species tree_species
<dbl> <chr> <chr> <chr>
1 2 control pine <NA>
2 3 control pine <NA>
3 2 CO2 pine <NA>
4 3 CO2 pine <NA>
5 2 control <NA> beech
6 3 control <NA> beech
7 2 CO2 <NA> beech
8 3 CO2 <NA> beech
Mini-exercise
How would you fix the problem?
# A tibble: 4 × 2
year irrigation
<dbl> <chr>
1 2023 yes
2 2023 no
3 2023 yes
4 2023 no
bind_cols(dat_pine, dat2)# A tibble: 4 × 5
temp treatment tree_species year irrigation
<dbl> <chr> <chr> <dbl> <chr>
1 2 control pine 2023 yes
2 3 control pine 2023 no
3 2 CO2 pine 2023 yes
4 3 CO2 pine 2023 no
Now it is your turn!
Exercise: Generating experimental treatments
Wrangling with data in R