Using the given code, answer the questions below.
library(tidyverse)
class_roster <- read.csv("~/R/busStat/Data/classRoster02.csv") %>%
as_tibble()
class_roster
## # A tibble: 30 x 6
## X Student Class Major income fav_carcompany
## <int> <fct> <fct> <fct> <int> <fct>
## 1 1 Scott Sophomore Marketing 1010 bmw
## 2 2 Colette Sophomore Business Administration 920 ""
## 3 3 Niti Senior Business Administration 1031 ""
## 4 4 Tyler Sophomore Management 1064 audi
## 5 5 Ryan Sophomore Undeclared 1021 tesla
## 6 6 Jack Sophomore Business Administration 1053 ford
## 7 7 Michael Sophomore Business Administration 1001 mercedes
## 8 8 Brianna Sophomore Marketing 1156 pagani
## 9 9 Trevor Sophomore Sports Management 1019 mclaren
## 10 10 Connor Sophomore Sports Management 848 aston martin
## # ... with 20 more rows
What is each students favorite car company?
Each row represents a student.
Class, major, income, and favorite car company.
Character
The class roster is a data frame because it can hold more than one data type.
Hint: Use View(). Scott is sophmore majoring in marketing with an income of $1010 and his favorite car company is BMW.
Hint: Use count(). There are 23 values in the new variable.
class_roster%>% count(fav_carcompany, sort = TRUE)
## # A tibble: 23 x 2
## fav_carcompany n
## <fct> <int>
## 1 honda 3
## 2 "" 2
## 3 audi 2
## 4 bmw 2
## 5 ford 2
## 6 subaru 2
## 7 acura 1
## 8 alfa romeo 1
## 9 aston martin 1
## 10 chevy 1
## # ... with 13 more rows
Hint: Refer to the ggplot2 cheatsheet. Google it. See the section for One Variable. Note that there are two different cases: 1) Continuous and 2) Discrete. The type of chart you can use depends on what type of data your variable is.
class_roster%>%
ggplot(aes(fav_carcompany)) +
geom_bar()
Hint: Use dplyr::group_by in addition to count().
class_roster%>%
group_by(Major)%>%
count(fav_carcompany, sort = TRUE)
## # A tibble: 29 x 3
## # Groups: Major [7]
## Major fav_carcompany n
## <fct> <fct> <int>
## 1 Business Administration "" 2
## 2 Accounting honda 1
## 3 Business Administration audi 1
## 4 Business Administration chevy 1
## 5 Business Administration ford 1
## 6 Business Administration honda 1
## 7 Business Administration land rover 1
## 8 Business Administration mercedes 1
## 9 Business Administration toyota 1
## 10 Interdisciplinary Studies ford 1
## # ... with 19 more rows
Hint: Use ggplot2::facet_wrap. Refer to the ggplot2 cheatsheet. See the section for Faceting.
class_roster%>%
ggplot(aes(fav_carcompany, Major)) +
geom_point() +
facet_wrap(~ Major)