Fix code language
This commit is contained in:
@@ -19,7 +19,7 @@ Introduction</h1>
|
||||
Prerequisites</h2>
|
||||
<p>In this chapter we’ll focus on tidyr, a package that provides a bunch of tools to help tidy up your messy datasets. tidyr is a member of the core tidyverse.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">library(tidyverse)</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">library(tidyverse)</pre>
|
||||
</div>
|
||||
<p>From this chapter on, we’ll suppress the loading message from <code><a href="https://tidyverse.tidyverse.org">library(tidyverse)</a></code>.</p>
|
||||
</section>
|
||||
@@ -32,7 +32,7 @@ Tidy data</h1>
|
||||
|
||||
<!-- TODO redraw as tables -->
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">table1
|
||||
<pre data-type="programlisting" data-code-language="r">table1
|
||||
#> # A tibble: 6 × 4
|
||||
#> country year cases population
|
||||
#> <chr> <int> <int> <int>
|
||||
@@ -99,7 +99,7 @@ table4b # population
|
||||
<li><p>There’s a specific advantage to placing variables in columns because it allows R’s vectorised nature to shine. As you learned in <a href="#sec-mutate" data-type="xref">#sec-mutate</a> and <a href="#sec-summarize" data-type="xref">#sec-summarize</a>, most built-in R functions work with vectors of values. That makes transforming tidy data feel particularly natural.</p></li>
|
||||
</ol><p>dplyr, ggplot2, and all the other packages in the tidyverse are designed to work with tidy data. Here are a couple of small examples showing how you might work with <code>table1</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit"># Compute rate per 10,000
|
||||
<pre data-type="programlisting" data-code-language="r"># Compute rate per 10,000
|
||||
table1 |>
|
||||
mutate(
|
||||
rate = cases / population * 10000
|
||||
@@ -164,7 +164,7 @@ Pivoting</h1>
|
||||
Data in column names</h2>
|
||||
<p>The <code>billboard</code> dataset records the billboard rank of songs in the year 2000:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">billboard
|
||||
<pre data-type="programlisting" data-code-language="r">billboard
|
||||
#> # A tibble: 317 × 79
|
||||
#> artist track date.ent…¹ wk1 wk2 wk3 wk4 wk5 wk6 wk7 wk8
|
||||
#> <chr> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
|
||||
@@ -192,7 +192,7 @@ Data in column names</h2>
|
||||
<code>values_to</code> names the variable stored in the cell values, here <code>"rank"</code>.</li>
|
||||
</ul><p>That gives the following call:</p>
|
||||
<div class="cell" data-r.options="{"pillar.print_min":10}">
|
||||
<pre data-type="programlisting" data-code-language="downlit">billboard |>
|
||||
<pre data-type="programlisting" data-code-language="r">billboard |>
|
||||
pivot_longer(
|
||||
cols = starts_with("wk"),
|
||||
names_to = "week",
|
||||
@@ -215,7 +215,7 @@ Data in column names</h2>
|
||||
</div>
|
||||
<p>What happens if a song is in the top 100 for less than 76 weeks? Take 2 Pac’s “Baby Don’t Cry”, for example. The above output suggests that it was only the top 100 for 7 weeks, and all the remaining weeks are filled in with missing values. These <code>NA</code>s don’t really represent unknown observations; they’re forced to exist by the structure of the dataset<span data-type="footnote">We’ll come back to this idea in <a href="#chp-missing-values" data-type="xref">#chp-missing-values</a>.</span>, so we can ask <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code> to get rid of them by setting <code>values_drop_na = TRUE</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">billboard |>
|
||||
<pre data-type="programlisting" data-code-language="r">billboard |>
|
||||
pivot_longer(
|
||||
cols = starts_with("wk"),
|
||||
names_to = "week",
|
||||
@@ -236,7 +236,7 @@ Data in column names</h2>
|
||||
<p>You might also wonder what happens if a song is in the top 100 for more than 76 weeks? We can’t tell from this data, but you might guess that additional columns <code>wk77</code>, <code>wk78</code>, … would be added to the dataset.</p>
|
||||
<p>This data is now tidy, but we could make future computation a bit easier by converting <code>week</code> into a number using <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> and <code><a href="https://readr.tidyverse.org/reference/parse_number.html">readr::parse_number()</a></code>. <code><a href="https://readr.tidyverse.org/reference/parse_number.html">parse_number()</a></code> is a handy function that will extract the first number from a string, ignoring all other text.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">billboard_tidy <- billboard |>
|
||||
<pre data-type="programlisting" data-code-language="r">billboard_tidy <- billboard |>
|
||||
pivot_longer(
|
||||
cols = starts_with("wk"),
|
||||
names_to = "week",
|
||||
@@ -260,7 +260,7 @@ billboard_tidy
|
||||
</div>
|
||||
<p>Now we’re in a good position to look at how song ranks vary over time by drawing a plot. The code is shown below and the result is <a href="#fig-billboard-ranks" data-type="xref">#fig-billboard-ranks</a>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">billboard_tidy |>
|
||||
<pre data-type="programlisting" data-code-language="r">billboard_tidy |>
|
||||
ggplot(aes(week, rank, group = track)) +
|
||||
geom_line(alpha = 1/3) +
|
||||
scale_y_reverse()</pre>
|
||||
@@ -278,7 +278,7 @@ billboard_tidy
|
||||
How does pivoting work?</h2>
|
||||
<p>Now that you’ve seen what pivoting can do for you, it’s worth taking a little time to gain some intuition about what it does to the data. Let’s start with a very simple dataset to make it easier to see what’s happening:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tribble(
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tribble(
|
||||
~var, ~col1, ~col2,
|
||||
"A", 1, 2,
|
||||
"B", 3, 4,
|
||||
@@ -287,7 +287,7 @@ How does pivoting work?</h2>
|
||||
</div>
|
||||
<p>Here we’ll say there are three variables: <code>var</code> (already in a variable), <code>name</code> (the column names in the column names), and <code>value</code> (the cell values). So we can tidy it with:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
pivot_longer(
|
||||
cols = col1:col2,
|
||||
names_to = "names",
|
||||
@@ -337,7 +337,7 @@ How does pivoting work?</h2>
|
||||
Many variables in column names</h2>
|
||||
<p>A more challenging situation occurs when you have multiple variables crammed into the column names. For example, take the <code>who2</code> dataset:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">who2
|
||||
<pre data-type="programlisting" data-code-language="r">who2
|
||||
#> # A tibble: 7,240 × 58
|
||||
#> country year sp_m_014 sp_m_1…¹ sp_m_…² sp_m_…³ sp_m_…⁴ sp_m_…⁵ sp_m_65
|
||||
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
|
||||
@@ -358,7 +358,7 @@ Many variables in column names</h2>
|
||||
<p>This dataset records information about tuberculosis data collected by the WHO. There are two columns that are already variables and are easy to interpret: <code>country</code> and <code>year</code>. They are followed by 56 columns like <code>sp_m_014</code>, <code>ep_m_4554</code>, and <code>rel_m_3544</code>. If you stare at these columns for long enough, you’ll notice there’s a pattern. Each column name is made up of three pieces separated by <code>_</code>. The first piece, <code>sp</code>/<code>rel</code>/<code>ep</code>, describes the method used for the <code>diagnosis</code>, the second piece, <code>m</code>/<code>f</code> is the <code>gender</code>, and the third piece, <code>014</code>/<code>1524</code>/<code>2535</code>/<code>3544</code>/<code>4554</code>/<code>65</code> is the <code>age</code> range.</p>
|
||||
<p>So in this case we have six variables: two variables are already columns, three variables are contained in the column name, and one variable is in the cell name. This requires two changes to our call to <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code>: <code>names_to</code> gets a vector of column names and <code>names_sep</code> describes how to split the variable name up into pieces:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">who2 |>
|
||||
<pre data-type="programlisting" data-code-language="r">who2 |>
|
||||
pivot_longer(
|
||||
cols = !(country:year),
|
||||
names_to = c("diagnosis", "gender", "age"),
|
||||
@@ -393,7 +393,7 @@ Many variables in column names</h2>
|
||||
Data and variable names in the column headers</h2>
|
||||
<p>The next step up in complexity is when the column names include a mix of variable values and variable names. For example, take the <code>household</code> dataset:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">household
|
||||
<pre data-type="programlisting" data-code-language="r">household
|
||||
#> # A tibble: 5 × 5
|
||||
#> family dob_child1 dob_child2 name_child1 name_child2
|
||||
#> <int> <date> <date> <chr> <chr>
|
||||
@@ -405,7 +405,7 @@ Data and variable names in the column headers</h2>
|
||||
</div>
|
||||
<p>This dataset contains data about five families, with the names and dates of birth of up to two children. The new challenge in this dataset is that the column names contain the names of two variables (<code>dob</code>, <code>name)</code> and the values of another (<code>child,</code> with values 1 and 2). To solve this problem we again need to supply a vector to <code>names_to</code> but this time we use the special <code>".value"</code> sentinel. This overrides the usual <code>values_to</code> argument to use the first component of the pivoted column name as a variable name in the output.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">household |>
|
||||
<pre data-type="programlisting" data-code-language="r">household |>
|
||||
pivot_longer(
|
||||
cols = !family,
|
||||
names_to = c(".value", "child"),
|
||||
@@ -444,7 +444,7 @@ Widening data</h2>
|
||||
<p>So far we’ve used <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code> to solve the common class of problems where values have ended up in column names. Next we’ll pivot (HA HA) to <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code>, which helps when one observation is spread across multiple rows. This seems to arise less commonly in the wild, but it does seem to crop up a lot when dealing with governmental data.</p>
|
||||
<p>We’ll start by looking at <code>cms_patient_experience</code>, a dataset from the Centers of Medicare and Medicaid services that collects data about patient experiences:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_experience
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_experience
|
||||
#> # A tibble: 500 × 5
|
||||
#> org_pac_id org_nm measure_cd measure_title prf_r…¹
|
||||
#> <chr> <chr> <chr> <chr> <dbl>
|
||||
@@ -458,7 +458,7 @@ Widening data</h2>
|
||||
</div>
|
||||
<p>An observation is an organisation, but each organisation is spread across six rows, with one row for each variable, or measure. We can see the complete set of values for <code>measure_cd</code> and <code>measure_title</code> by using <code><a href="https://dplyr.tidyverse.org/reference/distinct.html">distinct()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_experience |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_experience |>
|
||||
distinct(measure_cd, measure_title)
|
||||
#> # A tibble: 6 × 2
|
||||
#> measure_cd measure_title
|
||||
@@ -473,7 +473,7 @@ Widening data</h2>
|
||||
<p>Neither of these columns will make particularly great variable names: <code>measure_cd</code> doesn’t hint at the meaning of the variable and <code>measure_title</code> is a long sentence containing spaces. We’ll use <code>measure_cd</code> for now, but in a real analysis you might want to create your own variable names that are both short and meaningful.</p>
|
||||
<p><code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> has the opposite interface to <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code>: we need to provide the existing columns that define the values (<code>values_from</code>) and the column name (<code>names_from)</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_experience |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_experience |>
|
||||
pivot_wider(
|
||||
names_from = measure_cd,
|
||||
values_from = prf_rate
|
||||
@@ -493,7 +493,7 @@ Widening data</h2>
|
||||
</div>
|
||||
<p>The output doesn’t look quite right; we still seem to have multiple rows for each organization. That’s because, by default, <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> will attempt to preserve all the existing columns including <code>measure_title</code> which has six distinct observations for each organisations. To fix this problem we need to tell <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> which columns identify each row; in this case those are the variables starting with <code>"org"</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_experience |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_experience |>
|
||||
pivot_wider(
|
||||
id_cols = starts_with("org"),
|
||||
names_from = measure_cd,
|
||||
@@ -519,7 +519,7 @@ Widening data</h2>
|
||||
How does<code>pivot_wider()</code> work?</h2>
|
||||
<p>To understand how <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> works, let’s again start with a very simple dataset:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tribble(
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tribble(
|
||||
~id, ~name, ~value,
|
||||
"A", "x", 1,
|
||||
"B", "y", 2,
|
||||
@@ -530,7 +530,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
</div>
|
||||
<p>We’ll take the values from the <code>value</code> column and the names from the <code>name</code> column:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
pivot_wider(
|
||||
names_from = name,
|
||||
values_from = value
|
||||
@@ -544,7 +544,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
<p>The connection between the position of the row in the input and the cell in the output is weaker than in <code><a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer()</a></code> because the rows and columns in the output are primarily determined by the values of variables, not their locations.</p>
|
||||
<p>To begin the process <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> needs to first figure out what will go in the rows and columns. Finding the column names is easy: it’s just the values of <code>name</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
distinct(name)
|
||||
#> # A tibble: 3 × 1
|
||||
#> name
|
||||
@@ -555,7 +555,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
</div>
|
||||
<p>By default, the rows in the output are formed by all the variables that aren’t going into the names or values. These are called the <code>id_cols</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
select(-name, -value) |>
|
||||
distinct()
|
||||
#> # A tibble: 2 × 1
|
||||
@@ -566,7 +566,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
</div>
|
||||
<p><code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> then combines these results to generate an empty data frame:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
select(-name, -value) |>
|
||||
distinct() |>
|
||||
mutate(x = NA, y = NA, z = NA)
|
||||
@@ -579,7 +579,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
<p>It then fills in all the missing values using the data in the input. In this case, not every cell in the output has corresponding value in the input as there’s no entry for id “B” and name “z”, so that cell remains missing. We’ll come back to this idea that <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> can “make” missing values in <a href="#chp-missing-values" data-type="xref">#chp-missing-values</a>.</p>
|
||||
<p>You might also wonder what happens if there are multiple rows in the input that correspond to one cell in the output. The example below has two rows that correspond to id “A” and name “x”:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tribble(
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tribble(
|
||||
~id, ~name, ~value,
|
||||
"A", "x", 1,
|
||||
"A", "x", 2,
|
||||
@@ -590,7 +590,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
</div>
|
||||
<p>If we attempt to pivot this we get an output that contains list-columns, which you’ll learn more about in <a href="#chp-rectangling" data-type="xref">#chp-rectangling</a>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |> pivot_wider(
|
||||
<pre data-type="programlisting" data-code-language="r">df |> pivot_wider(
|
||||
names_from = name,
|
||||
values_from = value
|
||||
)
|
||||
@@ -611,7 +611,7 @@ How does<code>pivot_wider()</code> work?</h2>
|
||||
</div>
|
||||
<p>Since you don’t know how to work with this sort of data yet, you’ll want to follow the hint in the warning to figure out where the problem is:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
group_by(id, name) |>
|
||||
summarize(n = n(), .groups = "drop") |>
|
||||
filter(n > 1L)
|
||||
@@ -635,7 +635,7 @@ Untidy data</h1>
|
||||
Presenting data to humans</h2>
|
||||
<p>As you’ve seen, <code><a href="https://dplyr.tidyverse.org/reference/count.html">dplyr::count()</a></code> produces tidy data: it makes one row for each group, with one column for each grouping variable, and one column for the number of observations.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">diamonds |>
|
||||
<pre data-type="programlisting" data-code-language="r">diamonds |>
|
||||
count(clarity, color)
|
||||
#> # A tibble: 56 × 3
|
||||
#> clarity color n
|
||||
@@ -650,7 +650,7 @@ Presenting data to humans</h2>
|
||||
</div>
|
||||
<p>This is easy to visualize or summarize further, but it’s not the most compact form for display. You can use <code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> to create a form more suitable for display to other humans:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">diamonds |>
|
||||
<pre data-type="programlisting" data-code-language="r">diamonds |>
|
||||
count(clarity, color) |>
|
||||
pivot_wider(
|
||||
names_from = color,
|
||||
@@ -677,7 +677,7 @@ Multivariate statistics</h2>
|
||||
<p>Most classical multivariate statistical methods (like dimension reduction and clustering) require your data in matrix form, where each column is a time point, or a location, or a gene, or a species, but definitely not a variable. Sometimes these formats have substantial performance or space advantages, or sometimes they’re just necessary to get closer to the underlying matrix mathematics.</p>
|
||||
<p>We’re not going to cover these statistical methods here, but it is useful to know how to get your data into the form that they need. For example, let’s imagine you wanted to cluster the gapminder data to find countries that had similar progression of <code>gdpPercap</code> over time. To do this, we need one row for each country and one column for each year:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">library(gapminder)
|
||||
<pre data-type="programlisting" data-code-language="r">library(gapminder)
|
||||
|
||||
col_year <- gapminder |>
|
||||
mutate(gdpPercap = log10(gdpPercap)) |>
|
||||
@@ -701,7 +701,7 @@ col_year
|
||||
</div>
|
||||
<p><code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> produces a tibble where each row is labelled by the <code>country</code> variable. But most classic statistical algorithms don’t want the identifier as an explicit variable; they want as a <strong>row name</strong>. We can turn the <code>country</code> variable into row names with <code>column_to_rowname()</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">col_year <- col_year |>
|
||||
<pre data-type="programlisting" data-code-language="r">col_year <- col_year |>
|
||||
column_to_rownames("country")
|
||||
|
||||
head(col_year)
|
||||
@@ -723,11 +723,11 @@ head(col_year)
|
||||
<p>This makes a data frame, because tibbles don’t support row names<span data-type="footnote">tibbles don’t use row names because they only work for a subset of important cases: when observations can be identified by a single character vector.</span>.</p>
|
||||
<p>We’re now ready to cluster with (e.g.) <code><a href="https://rdrr.io/r/stats/kmeans.html">kmeans()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cluster <- stats::kmeans(col_year, centers = 6)</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">cluster <- stats::kmeans(col_year, centers = 6)</pre>
|
||||
</div>
|
||||
<p>Extracting the data out of this object into a form you can work with is a challenge you’ll need to come back to later in the book, once you’ve learned more about lists. But for now, you can get the clustering membership out with this code:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cluster_id <- cluster$cluster |>
|
||||
<pre data-type="programlisting" data-code-language="r">cluster_id <- cluster$cluster |>
|
||||
enframe() |>
|
||||
rename(country = name, cluster_id = value)
|
||||
cluster_id
|
||||
@@ -744,7 +744,7 @@ cluster_id
|
||||
</div>
|
||||
<p>You could then combine this back with the original data using one of the joins you’ll learn about in <a href="#chp-joins" data-type="xref">#chp-joins</a>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">gapminder |> left_join(cluster_id)
|
||||
<pre data-type="programlisting" data-code-language="r">gapminder |> left_join(cluster_id)
|
||||
#> Joining with `by = join_by(country)`
|
||||
#> # A tibble: 1,704 × 7
|
||||
#> country continent year lifeExp pop gdpPercap cluster_id
|
||||
@@ -764,7 +764,7 @@ cluster_id
|
||||
Pragmatic computation</h2>
|
||||
<p>Sometimes it’s just easier to answer a question using untidy data. For example, if you’re interested in just the total number of missing values in <code>cms_patient_experience</code>, it’s easier to work with the untidy form:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_experience |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_experience |>
|
||||
group_by(org_pac_id) |>
|
||||
summarize(
|
||||
n_miss = sum(is.na(prf_rate)),
|
||||
@@ -785,7 +785,7 @@ Pragmatic computation</h2>
|
||||
<p>So if you’re stuck figuring out how to do some computation, maybe it’s time to switch up the organisation of your data. For computations involving a fixed number of values (like computing differences or ratios), it’s usually easier if the data is in columns; for those with a variable number of values (like sums or means) it’s usually easier in rows. Don’t be afraid to untidy, transform, and re-tidy if needed.</p>
|
||||
<p>Let’s explore this idea by looking at <code>cms_patient_care</code>, which has a similar structure to <code>cms_patient_experience</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_care
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_care
|
||||
#> # A tibble: 252 × 5
|
||||
#> ccn facility_name measure_abbr score type
|
||||
#> <chr> <chr> <chr> <dbl> <chr>
|
||||
@@ -801,7 +801,7 @@ Pragmatic computation</h2>
|
||||
<ul><li>
|
||||
<p>If you want to compute the number of patients that answered yes to the question, you may pivot <code>type</code> into the columns:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_care |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_care |>
|
||||
pivot_wider(
|
||||
names_from = type,
|
||||
values_from = score
|
||||
@@ -824,7 +824,7 @@ Pragmatic computation</h2>
|
||||
<li>
|
||||
<p>If you want to display the distribution of each metric, you may keep it as is so you could facet by <code>measure_abbr</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_care |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_care |>
|
||||
filter(type == "observed") |>
|
||||
ggplot(aes(score)) +
|
||||
geom_histogram(binwidth = 2) +
|
||||
@@ -835,7 +835,7 @@ Pragmatic computation</h2>
|
||||
<li>
|
||||
<p>If you want to explore how different metrics are related, you may put the measure names in the columns so you could compare them in scatterplots.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">cms_patient_care |>
|
||||
<pre data-type="programlisting" data-code-language="r">cms_patient_care |>
|
||||
filter(type == "observed") |>
|
||||
select(-type) |>
|
||||
pivot_wider(
|
||||
|
||||
Reference in New Issue
Block a user