forked from BrunaLab/HeliconiaREU-Ellie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampling examples.Rmd
71 lines (49 loc) · 990 Bytes
/
Sampling examples.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
---
title: "R Notebook"
output: html_notebook
---
```{r setup}
knitr::opts_chunk$set(paged.print = FALSE)
```
```{r}
library(tidyverse)
```
Sampling random plant IDs
```{r}
plants <- unique(ha$ha_id_number)
plant_sample <- sample(plants, 10)
ha %>% filter(ha_id_number %in% plant_sample)
```
sampling years within plants
```{r}
ha %>% slice_sample(n = 10)
ha %>%
group_by(ha_id_number) %>%
slice_sample(n = 10)
```
reducing SPEI history
```{r}
ha %>%
mutate(spei_short = spei_history[ , 1:12])
```
take many samples (with for-loop)
```{r}
out <- vector("list", 10)
for (i in 1:10) {
plants <- unique(ha$ha_id_number)
plant_sample <- sample(plants, 10)
out[[i]] <-
ha %>%
filter(ha_id_number %in% plant_sample)
}
out
```
With the `purrr` package
```{r}
library(purrr)
out[[1]] %>%
summary(mean_ht = mean(ht, na.rm = TRUE))
means <- map_df(out, ~ .x %>% summarize(mean_ht = mean(ht, na.rm = TRUE)))
means
means %>% map_dbl(~.x$mean_ht)
```