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 03-dplyr.Rmd #539

Merged
merged 1 commit into from
Nov 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
13 changes: 9 additions & 4 deletions episodes/03-dplyr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ dataframe to adhere to (e.g. village name is Chirodzo):
filter(interviews, village == "Chirodzo")
```

You may also have noticed that the output from these call doesn't run off the
screen anymore. It's one of the advantages of `tbl_df` (also called tibble),
the central data class in the tidyverse, compared to normal dataframes in R.

We can also specify multiple conditions within the `filter()` function. We can
combine conditions using either "and" or "or" statements. In an "and"
statement, an observation (row) must meet **every** criteria to be included
Expand Down Expand Up @@ -365,9 +369,6 @@ interviews %>%
summarize(mean_no_membrs = mean(no_membrs))
```

You may also have noticed that the output from these calls doesn't run off the
screen anymore. It's one of the advantages of `tbl_df` over dataframe.

You can also group by multiple columns:

```{r, purl=FALSE}
Expand All @@ -376,7 +377,9 @@ interviews %>%
summarize(mean_no_membrs = mean(no_membrs))
```

Note that the output is a grouped tibble. To obtain an ungrouped tibble, use the
Note that the output is a grouped tibble of nine rows by three columns
which is indicated by the by two first lines with the `#`.
To obtain an ungrouped tibble, use the
`ungroup` function:

```{r, purl=FALSE}
Expand All @@ -386,6 +389,8 @@ interviews %>%
ungroup()
```

Notice that the second line with the `#` that previously indicated the grouping has
disappeared and we now only have a 9x3-tibble without grouping.
When grouping both by `village` and `membr_assoc`, we see rows in our table for
respondents who did not specify whether they were a member of an irrigation
association. We can exclude those data from our table using a filter step.
Expand Down
Loading