3 Group and summarise
(MA7419 / MA3419)
3.1 Overview
This week we’ll be doing more manipulation of tabular data using the dplyr
(Wickham et al. 2023) verbs group()
and summarise()
.
We’ll also be creating more bar charts using ggplot2
(Wickham 2016).
By the end of this week you’ll be able to:
- Group and summarise data in a tabular data set
- Use
table()
and functions from thejanitor
package to create cross-tab summaries - Use functions from the
forcats
package to work with factors
You’ll need to use the references and the R help system to develop the above skills as you work through the Week 3 task.
3.2 Reading
R for Data Science (Wickham and Grolemund 2017):
- Chapter 3 Data visualisation
- Chapter 5 Data transformation
- Chapter 6 Workflow: scripts
- Chapter 7 Exploratory Data Analysis
- Chapter 8 Workflow: projects
R Programming for Data Science (Peng 2020):
- Chapter 12 Managing Data Frames with the dplyr package
3.3 Making slides in R Markdown
You’ll need to make slides for the final assessment - so here’s a preview. It’s very easy - you can do it straight from the menu:
File > New File > R Markdown…
See the guide to producing slides (part of a larger introduction to using R Markdown from the makers of RStudio.)
3.4 Debugging
Code almost never works properly the first time you write it
Running into bugs and errors is frustrating, but it’s also an opportunity to learn a bit more.
Errors can be obscure, but they are usually not malicious or random.
If something has gone wrong, you can find out why it happened.
3.5 Knitting to HTML (hints)
make sure all your packages are up to date.
does all your .RMD code run properly (i.e. if you click “run all” does it run without any error messages) - if it doesn’t, fix it.
if (2) works, start a new R session on the server (for example, by closing down and restarting) - does the RMD code still run? If not, fix it.
once the RMD file runs, try clicking on knit to HTML, again, if that fails and you can’t fix it, ask for help.
If you can knit to HTMl, you can try knitting to Word and PDF.
If you can knit to Word, you can create a PDF file by exporting from Word
If you can’t create a PDF file in any other way you can try this as a last resort. If knitting to HTML works and opens an HTML file in a viewer window, click on “view in browser”. If you are using Chrome, you will be able to save as a PDF document under “print”. I advise installing Chrome for UoL work because that is the standard on University machines so most things are tested on it.
3.6 Week 3 Task
Some bigger files to work on this week, and some wider research required.
The task is on Blackboard and on the server at
/data/FDS/Scripts/
/data/FDS/Data/