Re-render book for O'Reilly

This commit is contained in:
Hadley Wickham
2023-01-12 17:22:57 -06:00
parent 28671ed8bd
commit 360d65ae47
113 changed files with 4957 additions and 2997 deletions

View File

@@ -3,8 +3,8 @@
<section id="introduction" data-type="sect1">
<h1>
Introduction</h1>
<p>So far you have learned about importing data from plain text files, e.g. <code>.csv</code> and <code>.tsv</code> files. Sometimes you need to analyze data that lives in a spreadsheet. In this chapter we will introduce you to tools for working with data in Excel spreadsheets and Google Sheets. This will build on much of what youve learned in <a href="#chp-data-import" data-type="xref">#chp-data-import</a> but we will also discuss additional considerations and complexities when working with data from spreadsheets.</p>
<p>If you or your collaborators are using spreadsheets for organizing data, we strongly recommend reading the paper “Data Organization in Spreadsheets” by Karl Broman and Kara Woo: <a href="https://doi.org/10.1080/00031305.2017.1375989" class="uri">https://doi.org/10.1080/00031305.2017.1375989</a>. The best practices presented in this paper will save you much headache down the line when you import the data from a spreadsheet into R to analyse and visualise.</p>
<p>So far, you have learned about importing data from plain text files, e.g., <code>.csv</code> and <code>.tsv</code> files. Sometimes you need to analyze data that lives in a spreadsheet. This chapter will introduce you to tools for working with data in Excel spreadsheets and Google Sheets. This will build on much of what youve learned in <a href="#chp-data-import" data-type="xref">#chp-data-import</a>, but we will also discuss additional considerations and complexities when working with data from spreadsheets.</p>
<p>If you or your collaborators are using spreadsheets for organizing data, we strongly recommend reading the paper “Data Organization in Spreadsheets” by Karl Broman and Kara Woo: <a href="https://doi.org/10.1080/00031305.2017.1375989" class="uri">https://doi.org/10.1080/00031305.2017.1375989</a>. The best practices presented in this paper will save you much headache when you import data from a spreadsheet into R to analyze and visualize.</p>
</section>
<section id="excel" data-type="sect1">
@@ -14,12 +14,12 @@ Excel</h1>
<section id="prerequisites" data-type="sect2">
<h2>
Prerequisites</h2>
<p>In this chapter, youll learn how to load data from Excel spreadsheets in R with the <strong>readxl</strong> package. This package is non-core tidyverse, so you need to load it explicitly but it is installed automatically when you install the tidyverse package.</p>
<p>In this section, youll learn how to load data from Excel spreadsheets in R with the <strong>readxl</strong> package. This package is non-core tidyverse, so you need to load it explicitly, but it is installed automatically when you install the tidyverse package.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">library(readxl)
library(tidyverse)</pre>
</div>
<p><strong>xlsx</strong> and <strong>XLConnect</strong> can be used for reading data from and writing data to Excel spreadsheets. However, these two packages require Java installed on your machine and the rJava package. Due to potential challenges with installation, we recommend using alternative packages weve introduced in this chapter.</p>
<p><strong>openxlsx</strong>, <strong>xlsx</strong>, and <strong>XLConnect</strong> can also be used for reading data from and writing data to Excel spreadsheets. We will discuss openxlsx in <a href="#sec-writing-to-excel" data-type="xref">#sec-writing-to-excel</a>. The latter two packages require Java installed on your machine and the rJava package. Due to potential challenges with installation, we recommend using alternative packages were introducing in this chapter.</p>
</section>
<section id="getting-started" data-type="sect2">
@@ -42,7 +42,7 @@ Reading spreadsheets</h2>
<div class="cell">
<div class="cell-output-display">
<figure id="fig-students-excel"><p><img src="images/import-spreadsheets-students.png" alt="A look at the students spreadsheet in Excel. The spreadsheet contains information on 6 students, their ID, full name, favourite food, meal plan, and age." width="1200"/></p>
<figure id="fig-students-excel"><p><img src="screenshots/import-spreadsheets-students.png" alt="A look at the students spreadsheet in Excel. The spreadsheet contains information on 6 students, their ID, full name, favourite food, meal plan, and age." width="1200"/></p>
<figcaption>Spreadsheet called students.xlsx in Excel.</figcaption>
</figure>
</div>
@@ -81,9 +81,9 @@ Reading spreadsheets</h2>
#&gt; 4 3 Jayendra Lyne N/A Breakfast and lunch 7
#&gt; 5 4 Leon Rossini Anchovies Lunch only &lt;NA&gt;
#&gt; 6 5 Chidiegwu Dunkel Pizza Breakfast and lunch five
#&gt; # … with 1 more row</pre>
#&gt; 7 6 Güvenç Attila Ice cream Lunch only 6</pre>
</div>
<p>Unfortunately, this didnt quite do the trick. You now have the variable names we want, but what was previously the header row now shows up as the first observation in the data. You can explicitly skip that row using the <code>skip</code> argument.</p>
<p>Unfortunately, this didnt quite do the trick. We now have the variable names we want, but what was previously the header row now shows up as the first observation in the data. You can explicitly skip that row using the <code>skip</code> argument.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_excel(
"data/students.xlsx",
@@ -102,7 +102,7 @@ Reading spreadsheets</h2>
</div>
</li>
<li>
<p>In the <code>favourite_food</code> column, one of the observations is <code>N/A</code>, which stands for “not available” but its currently not recognized as an <code>NA</code> (note the contrast between this <code>N/A</code> and the age of the fourth student in the list). You can specify which character strings should be recognized as <code>NA</code>s with the <code>na</code> argument. By default, only <code>""</code> (empty string, or, in the case of reading from a spreadsheet, an empty cell) is recognized as an <code>NA</code>.</p>
<p>In the <code>favourite_food</code> column, one of the observations is <code>N/A</code>, which stands for “not available” but its currently not recognized as an <code>NA</code> (note the contrast between this <code>N/A</code> and the age of the fourth student in the list). You can specify which character strings should be recognized as <code>NA</code>s with the <code>na</code> argument. By default, only <code>""</code> (empty string, or, in the case of reading from a spreadsheet, an empty cell or a cell with the formula <code>=NA()</code>) is recognized as an <code>NA</code>.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_excel(
"data/students.xlsx",
@@ -170,35 +170,35 @@ students
#&gt; 6 6 Güvenç Attila Ice cream Lunch only 6</pre>
</div>
</li>
</ol><p>It took us multiple steps and trial-and-error to load the data in exactly the format we want, and this is not unexpected. Data science is an iterative process. There is no way to know exactly what the data will look like until you load it and take a look at it. Well, there is one way, actually. You can open the file in Excel and take a peek. That might be tempting, but its strongly not recommended. <!--# TO DO: Provide reason why it's not recommended. --> Instead, you should not be afraid of doing what we did here: load the data, take a peek, make adjustments to your code, load it again, and repeat until youre happy with the result.</p>
</ol><p>It took us multiple steps and trial-and-error to load the data in exactly the format we want, and this is not unexpected. Data science is an iterative process, and the process of iteration can be even more tedious when reading data in from spreadsheets compared to other plain text, rectangular data files because humans tend to input data into spreadsheets and use them not just for data storage but also for sharing and communication.</p>
<p>There is no way to know exactly what the data will look like until you load it and take a look at it. Well, there is one way, actually. You can open the file in Excel and take a peek. If youre going to do so, we recommend making a copy of the Excel file to open and browse interactively while leaving the original data file untouched and reading into R from the untouched file. This will ensure you dont accidentally overwrite anything in the spreadsheet while inspecting it. You should also not be afraid of doing what we did here: load the data, take a peek, make adjustments to your code, load it again, and repeat until youre happy with the result.</p>
</section>
<section id="reading-individual-sheets" data-type="sect2">
<section id="reading-worksheets" data-type="sect2">
<h2>
Reading individual sheets</h2>
<p>An important feature that distinguishes spreadsheets from flat files is the notion of multiple sheets. <a href="#fig-penguins-islands" data-type="xref">#fig-penguins-islands</a> shows an Excel spreadsheet with multiple sheets. The data come from the <strong>palmerpenguins</strong> package. Each sheet contains information on penguins from a different island where data were collected.</p>
Reading worksheets</h2>
<p>An important feature that distinguishes spreadsheets from flat files is the notion of multiple sheets, called worksheets. <a href="#fig-penguins-islands" data-type="xref">#fig-penguins-islands</a> shows an Excel spreadsheet with multiple worksheets. The data come from the <strong>palmerpenguins</strong> package. Each worksheet contains information on penguins from a different island where data were collected.</p>
<div class="cell">
<div class="cell-output-display">
<figure id="fig-penguins-islands"><p><img src="images/import-spreadsheets-penguins-islands.png" alt="A look at the penguins spreadsheet in Excel. The spreadsheet contains has three sheets: Torgersen Island, Biscoe Island, and Dream Island." width="1514"/></p>
<figcaption>Spreadsheet called penguins.xlsx in Excel.</figcaption>
<figure id="fig-penguins-islands"><p><img src="screenshots/import-spreadsheets-penguins-islands.png" alt="A look at the penguins spreadsheet in Excel. The spreadsheet contains has three worksheets: Torgersen Island, Biscoe Island, and Dream Island." width="1514"/></p>
<figcaption>Spreadsheet called penguins.xlsx in Excel containing three worksheets.</figcaption>
</figure>
</div>
</div>
<p>You can read a single sheet from a spreadsheet with the <code>sheet</code> argument in <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<p>You can read a single worksheet from a spreadsheet with the <code>sheet</code> argument in <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_excel("data/penguins.xlsx", sheet = "Torgersen Island")
#&gt; # A tibble: 52 × 8
#&gt; species island bill_length_mm bill_dep…¹ flipp…² body_…³ sex year
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
#&gt; 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
#&gt; 2 Adelie Torgersen 39.5 17.399999… 186 3800 fema… 2007
#&gt; 3 Adelie Torgersen 40.299999999999997 18 195 3250 fema… 2007
#&gt; 4 Adelie Torgersen NA NA NA NA NA 2007
#&gt; 5 Adelie Torgersen 36.700000000000003 19.3 193 3450 fema… 2007
#&gt; 6 Adelie Torgersen 39.299999999999997 20.6 190 3650 male 2007
#&gt; # … with 46 more rows, and abbreviated variable names ¹bill_depth_mm,
#&gt; # ²flipper_length_mm, ³body_mass_g</pre>
#&gt; species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
#&gt; 1 Adelie Torgers 39.1 18.7 181 3750
#&gt; 2 Adelie Torgers 39.5 17.399999999… 186 3800
#&gt; 3 Adelie Torgers 40.2999999999… 18 195 3250
#&gt; 4 Adelie Torgers NA NA NA NA
#&gt; 5 Adelie Torgers 36.7000000000 19.3 193 3450
#&gt; 6 Adelie Torgers 39.2999999999 20.6 190 3650
#&gt; # … with 46 more rows, and 2 more variables: sex &lt;chr&gt;, year &lt;dbl&gt;</pre>
</div>
<p>Some variables that appear to contain numerical data are read in as characters due to the character string <code>"NA"</code> not being recognized as a true <code>NA</code>.</p>
<div class="cell">
@@ -206,28 +206,27 @@ Reading individual sheets</h2>
penguins_torgersen
#&gt; # A tibble: 52 × 8
#&gt; species island bill_length_mm bill_depth_mm flippe…¹ body_…² sex year
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt;
#&gt; 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
#&gt; 2 Adelie Torgersen 39.5 17.4 186 3800 fema… 2007
#&gt; 3 Adelie Torgersen 40.3 18 195 3250 fema… 2007
#&gt; 4 Adelie Torgersen NA NA NA NA &lt;NA&gt; 2007
#&gt; 5 Adelie Torgersen 36.7 19.3 193 3450 fema… 2007
#&gt; 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007
#&gt; # … with 46 more rows, and abbreviated variable names ¹flipper_length_mm,
#&gt; # ²body_mass_g</pre>
#&gt; species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1 Adelie Torgers 39.1 18.7 181 3750
#&gt; 2 Adelie Torgers 39.5 17.4 186 3800
#&gt; 3 Adelie Torgers 40.3 18 195 3250
#&gt; 4 Adelie Torgers NA NA NA NA
#&gt; 5 Adelie Torgers 36.7 19.3 193 3450
#&gt; 6 Adelie Torgers 39.3 20.6 190 3650
#&gt; # … with 46 more rows, and 2 more variables: sex &lt;chr&gt;, year &lt;dbl&gt;</pre>
</div>
<p>However, we cheated here a bit. We looked inside the Excel spreadsheet, which is not a recommended workflow. Instead, you can use <code><a href="https://readxl.tidyverse.org/reference/excel_sheets.html">excel_sheets()</a></code> to get information on all sheets in an Excel spreadsheet, and then read the one(s) youre interested in.</p>
<p>Alternatively, you can use <code><a href="https://readxl.tidyverse.org/reference/excel_sheets.html">excel_sheets()</a></code> to get information on all worksheets in an Excel spreadsheet, and then read the one(s) youre interested in.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">excel_sheets("data/penguins.xlsx")
#&gt; [1] "Torgersen Island" "Biscoe Island" "Dream Island"</pre>
</div>
<p>Once you know the names of the sheets, you can read them in individually with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<p>Once you know the names of the worksheets, you can read them in individually with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">penguins_biscoe &lt;- read_excel("data/penguins.xlsx", sheet = "Biscoe Island", na = "NA")
penguins_dream &lt;- read_excel("data/penguins.xlsx", sheet = "Dream Island", na = "NA")</pre>
</div>
<p>In this case the full penguins dataset is spread across three sheets in the spreadsheet. Each sheet has the same number of columns but different numbers of rows.</p>
<p>In this case the full penguins dataset is spread across three worksheets in the spreadsheet. Each worksheet has the same number of columns but different numbers of rows.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">dim(penguins_torgersen)
#&gt; [1] 52 8
@@ -241,16 +240,15 @@ dim(penguins_dream)
<pre data-type="programlisting" data-code-language="r">penguins &lt;- bind_rows(penguins_torgersen, penguins_biscoe, penguins_dream)
penguins
#&gt; # A tibble: 344 × 8
#&gt; species island bill_length_mm bill_depth_mm flippe…¹ body_…² sex year
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt;
#&gt; 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007
#&gt; 2 Adelie Torgersen 39.5 17.4 186 3800 fema… 2007
#&gt; 3 Adelie Torgersen 40.3 18 195 3250 fema… 2007
#&gt; 4 Adelie Torgersen NA NA NA NA &lt;NA&gt; 2007
#&gt; 5 Adelie Torgersen 36.7 19.3 193 3450 fema… 2007
#&gt; 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007
#&gt; # … with 338 more rows, and abbreviated variable names ¹flipper_length_mm,
#&gt; # ²body_mass_g</pre>
#&gt; species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1 Adelie Torgers 39.1 18.7 181 3750
#&gt; 2 Adelie Torgers 39.5 17.4 186 3800
#&gt; 3 Adelie Torgers 40.3 18 195 3250
#&gt; 4 Adelie Torgers NA NA NA NA
#&gt; 5 Adelie Torgers 36.7 19.3 193 3450
#&gt; 6 Adelie Torgers 39.3 20.6 190 3650
#&gt; # … with 338 more rows, and 2 more variables: sex &lt;chr&gt;, year &lt;dbl&gt;</pre>
</div>
<p>In <a href="#chp-iteration" data-type="xref">#chp-iteration</a> well talk about ways of doing this sort of task without repetitive code.</p>
</section>
@@ -262,7 +260,7 @@ Reading part of a sheet</h2>
<div class="cell">
<div class="cell-output-display">
<figure id="fig-deaths-excel"><p><img src="images/import-spreadsheets-deaths.png" alt="A look at the deaths spreadsheet in Excel. The spreadsheet has four rows on top that contain non-data information; the text 'For the same of consistency in the data layout, which is really a beautiful thing, I will keep making notes up here.' is spread across cells in these top four rows. Then, there is a data frame that includes information on deaths of 10 famous people, including their names, professions, ages, whether they have kids or not, date of birth and death. At the bottom, there are four more rows of non-data information; the text 'This has been really fun, but we're signing off now!' is spread across cells in these bottom four rows." width="1614"/></p>
<figure id="fig-deaths-excel"><p><img src="screenshots/import-spreadsheets-deaths.png" alt="A look at the deaths spreadsheet in Excel. The spreadsheet has four rows on top that contain non-data information; the text 'For the same of consistency in the data layout, which is really a beautiful thing, I will keep making notes up here.' is spread across cells in these top four rows. Then, there is a data frame that includes information on deaths of 10 famous people, including their names, professions, ages, whether they have kids or not, date of birth and death. At the bottom, there are four more rows of non-data information; the text 'This has been really fun, but we're signing off now!' is spread across cells in these bottom four rows." width="1614"/></p>
<figcaption>Spreadsheet called deaths.xlsx in Excel.</figcaption>
</figure>
</div>
@@ -294,30 +292,29 @@ deaths
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_excel(deaths_path, skip = 4)
#&gt; # A tibble: 14 × 6
#&gt; Name Profession Age `Has kids` `Date of birth` Date of dea…¹
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dttm&gt; &lt;chr&gt;
#&gt; 1 David Bowie musician 69 TRUE 1947-01-08 00:00:00 42379
#&gt; 2 Carrie Fisher actor 60 TRUE 1956-10-21 00:00:00 42731
#&gt; 3 Chuck Berry musician 90 TRUE 1926-10-18 00:00:00 42812
#&gt; 4 Bill Paxton actor 61 TRUE 1955-05-17 00:00:00 42791
#&gt; 5 Prince musician 57 TRUE 1958-06-07 00:00:00 42481
#&gt; 6 Alan Rickman actor 69 FALSE 1946-02-21 00:00:00 42383
#&gt; # … with 8 more rows, and abbreviated variable name ¹​`Date of death`</pre>
#&gt; Name Profession Age `Has kids` `Date of birth` `Date of death`
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dttm&gt; &lt;chr&gt;
#&gt; 1 David Bowie musician 69 TRUE 1947-01-08 00:00:00 42379
#&gt; 2 Carrie Fis actor 60 TRUE 1956-10-21 00:00:00 42731
#&gt; 3 Chuck Berry musician 90 TRUE 1926-10-18 00:00:00 42812
#&gt; 4 Bill Paxton actor 61 TRUE 1955-05-17 00:00:00 42791
#&gt; 5 Prince musician 57 TRUE 1958-06-07 00:00:00 42481
#&gt; 6 Alan Rickm actor 69 FALSE 1946-02-21 00:00:00 42383
#&gt; # … with 8 more rows</pre>
</div>
<p>We could also set <code>n_max</code> to omit the extraneous rows at the bottom.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_excel(deaths_path, skip = 4, n_max = 10)
#&gt; # A tibble: 10 × 6
#&gt; Name Profe…¹ Age Has k…² `Date of birth` `Date of death`
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dttm&gt; &lt;dttm&gt;
#&gt; 1 David Bowie musici 69 TRUE 1947-01-08 00:00:00 2016-01-10 00:00:00
#&gt; 2 Carrie Fisher actor 60 TRUE 1956-10-21 00:00:00 2016-12-27 00:00:00
#&gt; 3 Chuck Berry musici 90 TRUE 1926-10-18 00:00:00 2017-03-18 00:00:00
#&gt; 4 Bill Paxton actor 61 TRUE 1955-05-17 00:00:00 2017-02-25 00:00:00
#&gt; 5 Prince musici 57 TRUE 1958-06-07 00:00:00 2016-04-21 00:00:00
#&gt; 6 Alan Rickman actor 69 FALSE 1946-02-21 00:00:00 2016-01-14 00:00:00
#&gt; # … with 4 more rows, and abbreviated variable names ¹Profession,
#&gt; # ²​`Has kids`</pre>
#&gt; Name Profession Age `Has kids` `Date of birth` `Date of death`
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dttm&gt; &lt;dttm&gt;
#&gt; 1 David musician 69 TRUE 1947-01-08 00:00:00 2016-01-10 00:00:00
#&gt; 2 Carrie actor 60 TRUE 1956-10-21 00:00:00 2016-12-27 00:00:00
#&gt; 3 Chuck musician 90 TRUE 1926-10-18 00:00:00 2017-03-18 00:00:00
#&gt; 4 Bill P… actor 61 TRUE 1955-05-17 00:00:00 2017-02-25 00:00:00
#&gt; 5 Prince musician 57 TRUE 1958-06-07 00:00:00 2016-04-21 00:00:00
#&gt; 6 Alan R actor 69 FALSE 1946-02-21 00:00:00 2016-01-14 00:00:00
#&gt; # … with 4 more rows</pre>
</div>
<p>Another approach is using cell ranges. In Excel, the top left cell is <code>A1</code>. As you move across columns to the right, the cell label moves down the alphabet, i.e. <code>B1</code>, <code>C1</code>, etc. And as you move down a column, the number in the cell label increases, i.e. <code>A2</code>, <code>A3</code>, etc.</p>
<p>The data we want to read in starts in cell <code>A5</code> and ends in cell <code>F15</code>. In spreadsheet notation, this is <code>A5:F15</code>.</p>
@@ -333,26 +330,17 @@ deaths
<pre data-type="programlisting" data-code-language="r">read_excel(deaths_path, range = cell_rows(c(5, 15)))</pre>
</div>
</li>
<li>
<p>Specify cells that mark the top-left and bottom-right corners of the data the top-left corner, <code>A5</code>, translates to <code>c(5, 1)</code> (5th row down, 1st column) and the bottom-right corner, <code>F15</code>, translates to <code>c(15, 6)</code>:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_excel(deaths_path, range = cell_limits(c(5, 1), c(15, 6)))</pre>
</div>
</li>
</ul><p>If you have control over the sheet, an even better way is to create a “named range”. This is useful within Excel because named ranges help repeat formulas easier to create and they have some useful properties for creating dynamic charts and graphs as well. Even if youre not working in Excel, named ranges can be useful for identifying which cells to read into R. In the example above, the table were reading in is named <code>Table1</code>, so we can read it in with the following.</p>
<p><strong>TO DO:</strong> Add this once reading in named ranges are implemented in readxl.</p>
</section>
</ul></section>
<section id="data-types" data-type="sect2">
<h2>
Data types</h2>
<p>In CSV files, all values are strings. This is not particularly true to the data, but it is simple: everything is a string.</p>
<p>The underlying data in Excel spreadsheets is more complex. A cell can be one of five things:</p>
<ul><li><p>A logical, like TRUE / FALSE</p></li>
<ul><li><p>A boolean, like TRUE, FALSE, or NA</p></li>
<li><p>A number, like “10” or “10.5”</p></li>
<li><p>A date, which can also include time like “11/1/21” or “11/1/21 3:00 PM”</p></li>
<li><p>A string, like “ten”</p></li>
<li><p>A currency, which allows numeric values in a limited range and four decimal digits of fixed precision</p></li>
<li><p>A datetime, which can also include time like “11/1/21” or “11/1/21 3:00 PM”</p></li>
<li><p>A text string, like “ten”</p></li>
</ul><p>When working with spreadsheet data, its important to keep in mind that how the underlying data is stored can be very different than what you see in the cell. For example, Excel has no notion of an integer. All numbers are stored as floating points, but you can choose to display the data with a customizable number of decimal points. Similarly, dates are actually stored as numbers, specifically the number of seconds since January 1, 1970. You can customize how you display the date by applying formatting in Excel. Confusingly, its also possible to have something that looks like a number but is actually a string (e.g. type <code>'10</code> into a cell in Excel).</p>
<p>These differences between how the underlying data are stored vs. how theyre displayed can cause surprises when the data are loaded into R. By default readxl will guess the data type in a given column. A recommended workflow is to let readxl guess the column types, confirm that youre happy with the guessed column types, and if not, go back and re-import specifying <code>col_types</code> as shown in <a href="#sec-reading-spreadsheets" data-type="xref">#sec-reading-spreadsheets</a>.</p>
<p>Another challenge is when you have a column in your Excel spreadsheet that has a mix of these types, e.g. some cells are numeric, others text, others dates. When importing the data into R readxl has to make some decisions. In these cases you can set the type for this column to <code>"list"</code>, which will load the column as a list of length 1 vectors, where the type of each element of the vector is guessed.</p>
@@ -364,7 +352,7 @@ Data not in cell values</h2>
<p><strong>tidyxl</strong> is useful for importing non-tabular data from Excel files into R. For example, tidyxl doesnt coerce a pivot table into a data frame. See <a href="https://nacnudus.github.io/spreadsheet-munging-strategies/" class="uri">https://nacnudus.github.io/spreadsheet-munging-strategies/</a> for more on strategies for working with non-tabular data from Excel.</p>
</section>
<section id="writing-to-excel" data-type="sect2">
<section id="sec-writing-to-excel" data-type="sect2">
<h2>
Writing to Excel</h2>
<p>Lets create a small data frame that we can then write out. Note that <code>item</code> is a factor and <code>quantity</code> is an integer.</p>
@@ -391,7 +379,7 @@ write_xlsx(bake_sale, path = "data/bake-sale.xlsx")</pre>
<div class="cell">
<div class="cell-output-display">
<figure id="fig-bake-sale-excel"><p><img src="images/import-spreadsheets-bake-sale.png" alt="Bake sale data frame created earlier in Excel." width="917"/></p>
<figure id="fig-bake-sale-excel"><p><img src="screenshots/import-spreadsheets-bake-sale.png" alt="Bake sale data frame created earlier in Excel." width="917"/></p>
<figcaption>Spreadsheet called bake_sale.xlsx in Excel.</figcaption>
</figure>
</div>
@@ -470,7 +458,7 @@ writeDataTable(
<div class="cell">
<div class="cell-output-display">
<figure id="fig-penguins-species"><p><img src="images/import-spreadsheets-penguins-species.png" alt="A look at the penguins spreadsheet in Excel. The spreadsheet contains has three sheets: Torgersen Island, Biscoe Island, and Dream Island." width="1106"/></p>
<figure id="fig-penguins-species"><p><img src="screenshots/import-spreadsheets-penguins-species.png" alt="A look at the penguins spreadsheet in Excel. The spreadsheet contains has three sheets: Torgersen Island, Biscoe Island, and Dream Island." width="1106"/></p>
<figcaption>Spreadsheet called penguins.xlsx in Excel.</figcaption>
</figure>
</div>
@@ -481,52 +469,272 @@ writeDataTable(
<section id="exercises" data-type="sect2">
<h2>
Exercises</h2>
<ol type="1"><li>Recreate the <code>bake_sale</code> data frame, write it out to an Excel file using the <code><a href="https://rdrr.io/pkg/openxlsx/man/write.xlsx.html">write.xlsx()</a></code> function from the openxlsx package.</li>
<li>What happens if you try to read in a file with <code>.xlsx</code> extension with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_xls()</a></code>?</li>
</ol><!--# Need moar exercises --></section>
<ol type="1"><li>
<p>In an Excel file, create the following dataset and save it as <code>survey.xlsx</code>. Alternatively, you can download it as an Excel file from <a href="https://docs.google.com/spreadsheets/d/1yc5gL-a2OOBr8M7B3IsDNX5uR17vBHOyWZq6xSTG2G8/edit?usp=sharing">here</a>.</p>
<div class="cell">
<div class="cell-output-display">
<p><img src="screenshots/import-spreadsheets-survey.png" alt="A spreadsheet with 3 columns (group, subgroup, and id) and 12 rows. The group column has two values: 1 (spanning 7 merged rows) and 2 (spanning 5 merged rows). The subgroup column has four values: A (spanning 3 merged rows), B (spanning 4 merged rows), A (spanning 2 merged rows), and B (spanning 3 merged rows). The id column has twelve values, numbers 1 through 12." width="263"/></p>
</div>
</div>
<p>Then, read it into R, with <code>survey_id</code> as a character variable and <code>n_pets</code> as a numerical variable. Hint: You will need to convert “none” to 0.</p>
<div class="cell">
<pre><code>#&gt; # A tibble: 6 × 2
#&gt; survey_id n_pets
#&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1 1 0
#&gt; 2 2 1
#&gt; 3 3 NA
#&gt; 4 4 2
#&gt; 5 5 2
#&gt; 6 6 NA</code></pre>
</div>
</li>
<li>
<p>In another Excel file, create the following dataset and save it as <code>roster.xlsx</code>. Alternatively, you can download it as an Excel file from <a href="https://docs.google.com/spreadsheets/d/1LgZ0Bkg9d_NK8uTdP2uHXm07kAlwx8-Ictf8NocebIE/edit?usp=sharing">here</a>.</p>
<div class="cell">
<div class="cell-output-display">
<p><img src="screenshots/import-spreadsheets-roster.png" alt="A spreadsheet with 3 columns (group, subgroup, and id) and 12 rows. The group column has two values: 1 (spanning 7 merged rows) and 2 (spanning 5 merged rows). The subgroup column has four values: A (spanning 3 merged rows), B (spanning 4 merged rows), A (spanning 2 merged rows), and B (spanning 3 merged rows). The id column has twelve values, numbers 1 through 12." width="255"/></p>
</div>
</div>
<p>Then, read it into R. The resulting data frame should be called <code>roster</code> and should look like the following.</p>
<div class="cell">
<pre><code>#&gt; # A tibble: 12 × 3
#&gt; group subgroup id
#&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt;
#&gt; 1 1 A 1
#&gt; 2 1 A 2
#&gt; 3 1 A 3
#&gt; 4 1 B 4
#&gt; 5 1 B 5
#&gt; 6 1 B 6
#&gt; 7 1 B 7
#&gt; 8 2 A 8
#&gt; 9 2 A 9
#&gt; 10 2 B 10
#&gt; 11 2 B 11
#&gt; 12 2 B 12</code></pre>
</div>
</li>
<li>
<p>In a new Excel file, create the following dataset and save it as <code>sales.xlsx</code>. Alternatively, you can download it as an Excel file from <a href="https://docs.google.com/spreadsheets/d/1oCqdXUNO8JR3Pca8fHfiz_WXWxMuZAp3YiYFaKze5V0/edit?usp=sharing">here</a>.</p>
<div class="cell">
<div class="cell-output-display">
<p><img src="screenshots/import-spreadsheets-sales.png" alt="A spreadsheet with 2 columns and 13 rows. The first two rows have text containing information about the sheet. Row 1 says &quot;This file contains information on sales&quot;. Row 2 says &quot;Data are organized by brand name, and for each brand, we have the ID number for the item sold, and how many are sold.&quot;. Then there are two empty rows, and then 9 rows of data." width="317"/></p>
</div>
</div>
<p>a. Read <code>sales.xlsx</code> in and save as <code>sales</code>. The data frame should look like the following, with <code>id</code> and <code>n</code> as column names and with 9 rows.</p>
<div class="cell">
<pre><code>#&gt; # A tibble: 9 × 2
#&gt; id n
#&gt; &lt;chr&gt; &lt;chr&gt;
#&gt; 1 Brand 1 n
#&gt; 2 1234 8
#&gt; 3 8721 2
#&gt; 4 1822 3
#&gt; 5 Brand 2 n
#&gt; 6 3333 1
#&gt; 7 2156 3
#&gt; 8 3987 6
#&gt; 9 3216 5</code></pre>
</div>
<p>b. Modify <code>sales</code> further to get it into the following tidy format with three columns (<code>brand</code>, <code>id</code>, and <code>n</code>) and 7 rows of data. Note that <code>id</code> and <code>n</code> are numeric, <code>brand</code> is a character variable.</p>
<div class="cell">
<pre><code>#&gt; # A tibble: 7 × 3
#&gt; brand id n
#&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1 Brand 1 1234 8
#&gt; 2 Brand 1 8721 2
#&gt; 3 Brand 1 1822 3
#&gt; 4 Brand 2 3333 1
#&gt; 5 Brand 2 2156 3
#&gt; 6 Brand 2 3987 6
#&gt; 7 Brand 2 3216 5</code></pre>
</div>
</li>
<li><p>Recreate the <code>bake_sale</code> data frame, write it out to an Excel file using the <code><a href="https://rdrr.io/pkg/openxlsx/man/write.xlsx.html">write.xlsx()</a></code> function from the openxlsx package.</p></li>
<li><p>In <a href="#chp-data-import" data-type="xref">#chp-data-import</a> you learned about the <code><a href="https://rdrr.io/pkg/janitor/man/clean_names.html">janitor::clean_names()</a></code> function to turn columns names into snake case. Read the <code>students.xlsx</code> file that we introduced earlier in this section and use this function to “clean” the column names.</p></li>
<li><p>What happens if you try to read in a file with <code>.xlsx</code> extension with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_xls()</a></code>?</p></li>
</ol></section>
</section>
<section id="google-sheets" data-type="sect1">
<h1>
Google Sheets</h1>
<!--# TO DO: Write section. -->
<section id="prerequisites-1" data-type="sect2">
<h2>
Prerequisites</h2>
<p>TO DO:</p>
<ul><li>use googlesheets4</li>
<li>why 4?</li>
</ul></section>
<p>This section will also focus on spreadsheets, but this time youll be loading data from a Google Sheet with the <strong>googlesheets4</strong> package. This package is non-core tidyverse as well, you need to load it explicitly.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">library(googlesheets4)
library(tidyverse)</pre>
</div>
<p>A quick note about the name of the package: googlesheets4 uses v4 of the <a href="https://developers.google.com/sheets/api/">Sheets API v4</a> to provide an R interface to Google Sheets, hence the name.</p>
</section>
<section id="getting-started-1" data-type="sect2">
<h2>
Getting started</h2>
<p>TO DO:</p>
<ul><li>reading from public sheet with <code>read_sheet()</code> and <code>read_range()</code>
</li>
</ul></section>
<section id="authentication" data-type="sect2">
<h2>
Authentication</h2>
<p>The main function of the googlesheets4 package is <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>, which reads a Google Sheet from a URL or a file id. This function also goes by the name <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">range_read()</a></code>.</p>
<p>You can also create a brand new sheet with <code><a href="https://googlesheets4.tidyverse.org/reference/gs4_create.html">gs4_create()</a></code> or write to an existing sheet with <code><a href="https://googlesheets4.tidyverse.org/reference/sheet_write.html">sheet_write()</a></code> and friends.</p>
<p>In this section well work with the same datasets as the ones in the Excel section to highlight similarities and differences between workflows for reading data from Excel and Google Sheets. readxl and googlesheets4 packages are both designed to mimic the functionality of the readr package, which provides the <code><a href="https://readr.tidyverse.org/reference/read_delim.html">read_csv()</a></code> function youve seen in <a href="#chp-data-import" data-type="xref">#chp-data-import</a>. Therefore, many of the tasks can be accomplished with simply swapping out <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> for <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>. However youll also see that Excel and Google Sheets dont behave in exactly the same way, therefore other tasks may require further updates to the function calls.</p>
</section>
<section id="read-sheets" data-type="sect2">
<h2>
Read sheets</h2>
<p><a href="#fig-students-googlesheets" data-type="xref">#fig-students-googlesheets</a> shows what the spreadsheet were going to read into R looks like in Google Sheets. This is the same dataset as in <a href="#fig-students-excel" data-type="xref">#fig-students-excel</a>, except its stored in a Google Sheet instead of Excel.</p>
<div class="cell">
<div class="cell-output-display">
<figure id="fig-students-googlesheets"><p><img src="screenshots/import-googlesheets-students.png" alt="A look at the students spreadsheet in Google Sheets. The spreadsheet contains information on 6 students, their ID, full name, favourite food, meal plan, and age." width="986"/></p>
<figcaption>Google Sheet called students in a browser window.</figcaption>
</figure>
</div>
</div>
<p>The first argument to <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> is the URL of the file to read. You can also access this file via <a href="https://pos.it/r4ds-students" class="uri">https://pos.it/r4ds-students</a>, however note that at the time of writing this book you cant read a sheet directly from a short link.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">students &lt;- read_sheet("https://docs.google.com/spreadsheets/d/1V1nPp1tzOuutXFLb3G9Eyxi3qxeEhnOXUzL5_BcCQ0w/edit?usp=sharing")
#&gt; ✔ Reading from "students".
#&gt; ✔ Range 'Sheet1'.</pre>
</div>
<p><code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> will read the file in as a tibble.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">students
#&gt; # A tibble: 6 × 5
#&gt; `Student ID` `Full Name` favourite.food mealPlan AGE
#&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;list&gt;
#&gt; 1 1 Sunil Huffmann Strawberry yoghurt Lunch only &lt;dbl&gt;
#&gt; 2 2 Barclay Lynn French fries Lunch only &lt;dbl&gt;
#&gt; 3 3 Jayendra Lyne N/A Breakfast and lunch &lt;dbl&gt;
#&gt; 4 4 Leon Rossini Anchovies Lunch only &lt;NULL&gt;
#&gt; 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch &lt;chr&gt;
#&gt; 6 6 Güvenç Attila Ice cream Lunch only &lt;dbl&gt;</pre>
</div>
<p>Just like we did with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>, we can supply column names, NA strings, and column types to <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">students &lt;- read_sheet(
"https://docs.google.com/spreadsheets/d/1V1nPp1tzOuutXFLb3G9Eyxi3qxeEhnOXUzL5_BcCQ0w/edit?usp=sharing",
col_names = c("student_id", "full_name", "favourite_food", "meal_plan", "age"),
skip = 1,
na = c("", "N/A"),
col_types = c("dcccc")
) |&gt;
mutate(
age = if_else(age == "five", "5", age),
age = parse_number(age)
)
#&gt; ✔ Reading from "students".
#&gt; ✔ Range '2:10000000'.
students
#&gt; # A tibble: 6 × 5
#&gt; student_id full_name favourite_food meal_plan age
#&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
#&gt; 1 1 Sunil Huffmann Strawberry yoghurt Lunch only 4
#&gt; 2 2 Barclay Lynn French fries Lunch only 5
#&gt; 3 3 Jayendra Lyne &lt;NA&gt; Breakfast and lunch 7
#&gt; 4 4 Leon Rossini Anchovies Lunch only NA
#&gt; 5 5 Chidiegwu Dunkel Pizza Breakfast and lunch 5
#&gt; 6 6 Güvenç Attila Ice cream Lunch only 6</pre>
</div>
<p>Note that we defined column types a bit differently here, using short codes. For example, “dcccc” stands for “double, character, character, character, character”.</p>
<p>Its also possible to read individual sheets from Google Sheets as well. Lets read the penguins Google Sheet at <a href="https://pos.it/r4ds-penguins" class="uri">https://pos.it/r4ds-penguins</a>, and specifically the “Torgersen Island” sheet in it.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">read_sheet("https://docs.google.com/spreadsheets/d/1aFu8lnD_g0yjF5O-K6SFgSEWiHPpgvFCF0NY9D6LXnY/edit?usp=sharing", sheet = "Torgersen Island")
#&gt; ✔ Reading from "penguins".
#&gt; ✔ Range ''Torgersen Island''.
#&gt; # A tibble: 52 × 8
#&gt; species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;list&gt; &lt;list&gt; &lt;list&gt; &lt;list&gt;
#&gt; 1 Adelie Torgers… &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt;
#&gt; 2 Adelie Torgers… &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt;
#&gt; 3 Adelie Torgers… &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt;
#&gt; 4 Adelie Torgers… &lt;chr [1]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt; &lt;chr [1]&gt;
#&gt; 5 Adelie Torgers… &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt;
#&gt; 6 Adelie Torgers… &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt; &lt;dbl [1]&gt;
#&gt; # … with 46 more rows, and 2 more variables: sex &lt;chr&gt;, year &lt;dbl&gt;</pre>
</div>
<p>You can obtain a list of all sheets within a Google Sheet with <code><a href="https://googlesheets4.tidyverse.org/reference/sheet_properties.html">sheet_names()</a></code>:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">sheet_names("https://docs.google.com/spreadsheets/d/1aFu8lnD_g0yjF5O-K6SFgSEWiHPpgvFCF0NY9D6LXnY/edit?usp=sharing")
#&gt; [1] "Torgersen Island" "Biscoe Island" "Dream Island"</pre>
</div>
<p>Finally, just like with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code>, we can read in a portion of a Google Sheet by defining a <code>range</code> in <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code>. Note that were also using the <code><a href="https://googlesheets4.tidyverse.org/reference/gs4_examples.html">gs4_example()</a></code> function below to locate an example Google Sheet that comes with the googlesheets4 package.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">deaths_url &lt;- gs4_example("deaths")
deaths &lt;- read_sheet(deaths_url, range = "A5:F15")
#&gt; ✔ Reading from "deaths".
#&gt; ✔ Range 'A5:F15'.
deaths
#&gt; # A tibble: 10 × 6
#&gt; Name Profession Age `Has kids` `Date of birth` `Date of death`
#&gt; &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;lgl&gt; &lt;dttm&gt; &lt;dttm&gt;
#&gt; 1 David … musician 69 TRUE 1947-01-08 00:00:00 2016-01-10 00:00:00
#&gt; 2 Carrie… actor 60 TRUE 1956-10-21 00:00:00 2016-12-27 00:00:00
#&gt; 3 Chuck … musician 90 TRUE 1926-10-18 00:00:00 2017-03-18 00:00:00
#&gt; 4 Bill P… actor 61 TRUE 1955-05-17 00:00:00 2017-02-25 00:00:00
#&gt; 5 Prince musician 57 TRUE 1958-06-07 00:00:00 2016-04-21 00:00:00
#&gt; 6 Alan R… actor 69 FALSE 1946-02-21 00:00:00 2016-01-14 00:00:00
#&gt; # … with 4 more rows</pre>
</div>
</section>
<section id="write-sheets" data-type="sect2">
<h2>
Write sheets</h2>
<p>You can write from R to Google Sheets with <code><a href="https://googlesheets4.tidyverse.org/reference/sheet_write.html">write_sheet()</a></code>:</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">write_sheet(bake_sale, ss = "bake-sale")</pre>
</div>
<p>If youd like to write your data to a specific (work)sheet inside a Google Sheet, you can specify that with the <code>sheet</code> argument as well.</p>
<div class="cell">
<pre data-type="programlisting" data-code-language="r">write_sheet(bake_sale, ss = "bake-sale", sheet = "Sales")</pre>
</div>
</section>
<section id="authentication" data-type="sect2">
<h2>
Authentication</h2>
<p>While you can read from a public Google Sheet without authenticating with your Google account, reading a private sheet or writing to a sheet requires authentication so that googlesheets4 can view and manage <em>your</em> Google Sheets.</p>
<p>When you attempt to read in a sheet that requires authentication, googlesheets4 will direct you to a web browser with a prompt to sign in to your Google account and grant permission to operate on your behalf with Google Sheets. However, if you want to specify a specific Google account, authentication scope, etc. you can do so with <code><a href="https://googlesheets4.tidyverse.org/reference/gs4_auth.html">gs4_auth()</a></code>, e.g. <code>gs4_auth(email = "mine@example.com")</code>, which will force the use of a token associated with a specific email. For further authentication details, we recommend reading the documentation googlesheets4 auth vignette: <a href="https://googlesheets4.tidyverse.org/articles/auth.html" class="uri">https://googlesheets4.tidyverse.org/articles/auth.html</a>.</p>
</section>
<section id="exercises-1" data-type="sect2">
<h2>
Exercises</h2>
<ol type="1"><li><p>Read the <code>students</code> dataset from earlier in the chapter from Excel and also from Google Sheets, with no additional arguments supplied to the <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> and <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> functions. Are the resulting data frames in R exactly the same? If not, how are they different?</p></li>
<li><p>Read the Google Sheet titled survey from <a href="https://pos.it/r4ds-survey" class="uri">https://pos.it/r4ds-survey</a>, with <code>survey_id</code> as a character variable and <code>n_pets</code> as a numerical variable.</p></li>
<li>
<p>Read the Google Sheet titled roster from <a href="https://pos.it/r4ds-roster" class="uri">https://pos.it/r4ds-roster</a>. The resulting data frame should be called <code>roster</code> and should look like the following.</p>
<div class="cell">
<pre><code>#&gt; # A tibble: 12 × 3
#&gt; group subgroup id
#&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt;
#&gt; 1 1 A 1
#&gt; 2 1 A 2
#&gt; 3 1 A 3
#&gt; 4 1 B 4
#&gt; 5 1 B 5
#&gt; 6 1 B 6
#&gt; 7 1 B 7
#&gt; 8 2 A 8
#&gt; 9 2 A 9
#&gt; 10 2 B 10
#&gt; 11 2 B 11
#&gt; 12 2 B 12</code></pre>
</div>
</li>
</ol></section>
</section>
<section id="summary" data-type="sect1">
<h1>
Summary</h1>
<p>In this chapter you learned how to read data into R from spreadsheets: from Microsoft Excel with <code><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel()</a></code> from the readxl package and from Google Sheets with <code><a href="https://googlesheets4.tidyverse.org/reference/range_read.html">read_sheet()</a></code> from the googlesheets4 package. These functions work very similarly to each other and have similar arguments for specifying column names, NA strings, rows to skip on top of the file youre reading in, etc. Additionally, both functions make it possible to read a single sheet from a spreadsheet as well.</p>
<p>On the other hand, writing to an Excel file requires a different package and function (<code><a href="https://docs.ropensci.org/writexl/reference/write_xlsx.html">writexl::write_xlsx()</a></code>) while you can write to a Google Sheet with the googlesheets4 package, with <code><a href="https://googlesheets4.tidyverse.org/reference/sheet_write.html">write_sheet()</a></code>.</p>
<p>In the next chapter, youll learn about a different data source and how to read data from that source into R: databases.</p>
</section>
</section>
</section>