Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update aggregation materials to use drop_na #1057

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions materials/dplyr-aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,27 @@ size_abundance_data <- surveys |>
* *Open table*
* Why did we get `NA`?
* `mean(weight)` returns `NA` when `weight` has missing values (`NA`)
* Can fix using `mean(weight, na.rm = TRUE)`
* Can fix using `drop_na(weight)`

```r
size_abundance_data <- surveys |>
drop_na(weight) |>
group_by(plot_id, year) |>
summarize(abundance = n(),
avg_weight = mean(weight, na.rm = TRUE))
```

* Still has `NaN` for cases where no individuals have a weight
* Can filter using `!is.na`

```r
size_abundance_data <- surveys |>
group_by(plot_id, year) |>
summarize(abundance = n(),
avg_weight = mean(weight, na.rm = TRUE)) |>
filter(!is.na(avg_weight))
summarize(abundance = n(), avg_weight = mean(weight))
```

* Also note the message about "grouped output"
* It says that the resulting data frame is grouped by `year`
* When we group by more than one column the resulting data frame is grouped by all but the last group
* Can be useful in some more complicated circumstances
* Can also make things not work if functions don't support grouped data frames
* We can remove these groups by add `ungroup()` to the end of our pipeline
* To remove these groups add `ungroup()` to the end of the pipeline

```r
size_abundance_data <- surveys |>
drop_na(weight) |>
group_by(plot_id, year) |>
summarize(abundance = n(),
avg_weight = mean(weight, na.rm = TRUE)) |>
filter(!is.na(avg_weight)) |>
summarize(abundance = n(), avg_weight = mean(weight)) |>
ungroup()
```

Expand Down
Loading