Homework Module 1.3 Key

Your Name

Date

In the questions below, use R code to answer questions. For any non-coding questions, give your answer as a comment.

Run this code chunk first, to make the survey data available as the variable classData:

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
classData<-read_csv("teamAntarcticaData.csv")
Rows: 75 Columns: 12
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (7): Timestamp, school, swim, animals, parkaColor, teamFlag, distance
dbl (5): fishing, cold, remote, bedsideManner, cooking

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  1. Create a subset of classData of students who responded less than 3 on the fishing question, called poorFishing. How many reported less than 3?
poorFishing<-classData %>% filter(fishing<3)

nrow(poorFishing)
[1] 54
  1. Describe logical operators, and how they can be used in the filter function:
# Logical operators are a logical action or process, like "&" or "|". They allow you to have multiple conditions in the filter function.
  1. How many Lewis & Clark students voted to have the bear as the team flag? Show code below how you would derive your answer:
lcBear<-classData %>% 
  filter(school=="Lewis & Clark College" & teamFlag=="Bear")

nrow(lcBear)
[1] 9
  1. What is the “pipe” in R? What is it used for?
# The pipe, %>%, is a tidyverse convention that allows you to chain data and functions together. 
  1. Determine the mean and standard deviation of self-reported bedside manner, comparing Lewis & Clark to University of Arizona students:
bedsideMannerData<- classData %>% 
  group_by(school) %>% 
  summarize(bedsideAvg=mean(bedsideManner), bedsideSd=sd(bedsideManner))

bedsideMannerData
# A tibble: 2 × 3
  school                bedsideAvg bedsideSd
  <chr>                      <dbl>     <dbl>
1 Lewis & Clark College       3.62     0.922
2 University of Arizona       3.39     1.34 
  1. Create a data set showing the percentage of votes for team flag animal, just for Lewis & Clark students:
lc<-classData %>% 
  filter(school=="Lewis & Clark College")

lcTotal<-nrow(lc)

lcFlagPercentage<-lc %>% 
  group_by(teamFlag) %>% 
  summarize(percentage=n()/lcTotal)

lcFlagPercentage
# A tibble: 4 × 2
  teamFlag                             percentage
  <chr>                                     <dbl>
1 Bear                                      0.191
2 Penguin                                   0.532
3 Sea Spider (Pycnogonida - google it)      0.170
4 Seal                                      0.106