Skip to content

Commit

Permalink
Merge pull request #539 from XAM12/patch-1
Browse files Browse the repository at this point in the history
Update 03-dplyr.Rmd
  • Loading branch information
juanfung authored Nov 29, 2024
2 parents 216d1df + 6681fe0 commit e378141
Showing 1 changed file with 9 additions and 4 deletions.
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

0 comments on commit e378141

Please sign in to comment.