7  R Practice: Literate Analysis

Learning Objectives

  • Practice base R skills
  • Practice Markdown syntax
  • Work in Quarto document

7.1 Introduction

In this session of R practice, we will be working with the dataset: Tobias Schwoerer, Kevin Berry, and Jorene Joe. 2022. A household survey documenting experiences with coastal hazards in a western Alaska community (2021-2022). Arctic Data Center. doi:10.18739/A29Z90D3V..

We will be focusing on the initial survey results (Initial_Survey111721_ADC.csv). In this file, individual survey responses are oriented as rows, and the questions are oriented as columns. The column names are Q1, Q2, etc. Information about what was the asked question, and what the allowed values mean, are available in the metadata for each file. You can access the metadata for each file by clicking the “more info” link next to the file name at the top of the page.

Big Idea

The goal for this session is to practice downloading data, reading it into R in a Quarto document, using R commands to summarize a variable within the dataset, and practice formatting a Quarto document using Markdown syntax.

7.2 Exercise 1

Set up
  • Navigate to the dataset archived in the Artic Data Center repository.
  • Download the file Initial_Survey111721_ADC.csv
  • Upload the file to a data folder within your training project in the server.
    • For this you have to click on the Upload button on the Files Pane in RStudio.
    • A pop-up window will come up. Make sure that the Target directory (directory where you file is going to be saved to) is the data folder withing your RProject.
    • Click on Choose File and navigate to where Initial_Survey111721_ADC.csv was downloaded in you local computer. Click Open, then press OK.

You should now see the Initial_Survey111721_ADC.csv inside the data folder in your project.

7.2.1 Open a new Quarto Document

Step 1
  1. Create a new Quarto document. Give it a title something along the lines of “Practice Session”.

  2. Save it to your R project.

  3. Structure your document with the following headers:

  • Setup

  • Read data

  • Explore data

  • Calculate mean

  • Conclusion

Tip: In Markdown syntax, # indicates the level of header. # is a “level one header”, meaning the biggest font and the top of the hierarchy. ### is a level three header, and will show up nested below the # and ## headers.

7.2.2 Load necesary libraries

Step 2

Under the Setup header load the read package.

library(readr)

7.2.3 Read in the data

Step 3

Read in the data and store the data frame as survey_data.

survey_data <- read_csv("data/Initial_Survey111721_ADC.csv")

7.2.4 Explore the data

Step 4

Try functions like summary(), colnames(), str(), unique(). Feel free to use any other function you’d like to learn and explore this data set.

You can also try View() in the console. Note that if you include View() in your Quarto document it will cause an error when rendering.

summary(survey_data)
colnames(survey_data)
str(survey_data)
unique(survey_data$Q6_2)

7.2.5 Calculate the mean

Step 5

Calculate the mean of the answers to Question 3 and save this value into an object in your Global Environment.

Tip: Look at the help page if your answer isn’t what you expect, ?mean(). Does Q3 column have NA values?

q3_mean <- mean(survey_data$Q3, na.rm = TRUE)

7.2.6 Write a conclusion

Step 6

Interpret this value using the metadata for the table and write a conclusion based on your interpretation calling the mean value you calculated in text.

Your Markdown file should look something like this

The average score about respondents perspective in xyz is `r round(q3_mean)\. This means that …

7.2.7 Render and Code Chunk Options

Step 7

Render your Quarto file and make sure you get the output you are expecting. Are you running into any errors? If your file is not rendering as you expect discuss with your neighbor how you can improve your file.

Did you added any code chunk options? Review Section A Quarto Document on the Literate Analysis Lesson and decide what code chunck otion you want to include in your file.

7.3 Bonus

Go Further

What other ways might you summarize the answers to question 3? Explore!