This file contains a set of tasks that you need to complete in R for the lab assignment. The tasks may require you to add a code chuck, type code into a chunk, and/or execute code. Some tasks may also ask you to answer specific questions. Don’t forget that you need to acknowledge if you used any resources beyond class materials or got help to complete the assignment.
More information and examples that can help you with this assignment can be found in the file “GettingtoKnowYourDataTutorial.html”.
The data set you will use is different than the one used in the instructions. Pay attention to the differences in the Excel file name and any variable names. You will need to adjust your code accordingly.
Once you have completed the assignment, you will need to publish it to produce an html file. You will then need to upload the html file and this .Rmd file to AsULearn.
The first thing you need to do in this file is to add your name and date in the lines underneath this document’s title (see the code in lines 10 and 11). While you will change the things in lines 10 and 11, you should not add anything new in this file until after line 42. Do not delete anything in the file.
You need to identify and set your working directory, load packages,
and load your data in this section. In addition to the
openxlsx
package that we used in the Getting Started in R
lab, you also need to load the packages dplyr
and
tidyverse
. Remember that before you load a package for the
1st time, you need to install the package. The name of the Excel file is
different than what is in the instructions, you will need to adjust the
code to read in the Excel file that was downloaded as part of the zip
file.
install.packages("openxlsx")
##
## The downloaded binary packages are in
## /var/folders/76/w01_ncvd5pn4r8v5nxz870fh0000gn/T//RtmpftEpzL/downloaded_packages
install.packages("dplyr")
##
## The downloaded binary packages are in
## /var/folders/76/w01_ncvd5pn4r8v5nxz870fh0000gn/T//RtmpftEpzL/downloaded_packages
install.packages("tidyverse")
##
## The downloaded binary packages are in
## /var/folders/76/w01_ncvd5pn4r8v5nxz870fh0000gn/T//RtmpftEpzL/downloaded_packages
Insert your chunks of code here to identify and set your working directory, load packages, and load the data. I recommend doing one thing per chunk of code.
getwd()
## [1] "/Users/summersimpson/Downloads/GettingToKnowYourDataFall2025"
setwd("/Users/summersimpson/Downloads/GettingToKnowYourDataFall2025")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
If you are working in the cloud version of RStudio, you do not need to set the working directory because you will have had to load this file and the Excel file into the cloud to be able to access them. Instead right before your chunk of code write in all capital letters that you are using RStuiod in the cloud.
library(openxlsx)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.2 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.3.0
## ✔ purrr 1.1.0 ✔ tidyr 1.3.1
## ── 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
Display the first 15 observations of your data set.
Insert your chunk of code here to display your first 15 observations.
library(readxl)
BBQData <- read_excel("~/Downloads/GettingToKnowYourDataFall2025 2/BBQ_Assignment.xlsx")
dplyr::glimpse(BBQData)
## Rows: 15
## Columns: 13
## $ Sex <chr> "Male", "Female", "Female", "Male", "Female", "Female…
## $ Age <chr> "19", "24", "2", "50", "19", "20", "19", "23", "25", …
## $ Hometown <chr> "Eastern or Central NC", "Eastern or Central NC", "Ea…
## $ FavoriteMeat <chr> "beef brisket", "pulled pork", "pulled pork", "pulled…
## $ FavoriteSauce <chr> "Western style (with tomato)", "Eastern style (with n…
## $ Sweetness <dbl> 4, 1, 4, 3, 3, 4, 3, 3, 3, 2, 3, 4, 3, 3, 3
## $ FavoriteSide <chr> "fries", "other", "hush puppies", "fried okra", "bake…
## $ RestaurantCity <chr> "Wilmington", "Wilmington", "Angier ", "Wilson, NC", …
## $ RestaurantName <chr> "Jackson's", "Jackson's BBQ", "Stephenson’s BBQ", "Pa…
## $ MinutesDriving <chr> "20", "20", "20", "30", "0", "15", "20", "240", "30",…
## $ SandwichPrice <chr> "13", "10", "10", "15", "5", "3", "6", "15", "20", "1…
## $ DinnerPlatePrice <chr> "11", "15", "16", "20", "7", "18", "8", "20", "25", "…
## $ RibsPrice <chr> "15", "20", "20", "35", "9", "20", "10", "25", "30", …
# 4. Create a unique id
#Add a variable that is a unique id for each observation. Then take another look at the data. After creating the unique id variable, display the first 15 observations in your data set.
print(BBQData$DinnerPlatePrice)
## [1] "11" "15" "16" "20" "7" "18" "8" "20" "25" "90" "20" "20" "17" "15" "15"
head(BBQData, 10)
## # A tibble: 10 × 13
## Sex Age Hometown FavoriteMeat FavoriteSauce Sweetness FavoriteSide
## <chr> <chr> <chr> <chr> <chr> <dbl> <chr>
## 1 Male 19 Eastern or Ce… beef brisket Western styl… 4 fries
## 2 Female 24 Eastern or Ce… pulled pork Eastern styl… 1 other
## 3 Female 2 Eastern or Ce… pulled pork Eastern styl… 4 hush puppies
## 4 Male 50 Elsewhere pulled pork Western styl… 3 fried okra
## 5 Female 19 Eastern or Ce… pork ribs South Caroli… 3 baked beans
## 6 Female 20 Eastern or Ce… pulled pork Kansas style… 4 coleslaw
## 7 Other 19 Eastern or Ce… pulled pork Eastern styl… 3 hush puppies
## 8 Male 23 Elsewhere beef brisket Kansas style… 3 hush puppies
## 9 Female 25 Eastern or Ce… pulled pork Western styl… 3 hush puppies
## 10 Female 49 Elsewhere pulled pork Eastern styl… 2 fried okra
## # ℹ 6 more variables: RestaurantCity <chr>, RestaurantName <chr>,
## # MinutesDriving <chr>, SandwichPrice <chr>, DinnerPlatePrice <chr>,
## # RibsPrice <chr>
tail(BBQData, 5)
## # A tibble: 5 × 13
## Sex Age Hometown FavoriteMeat FavoriteSauce Sweetness FavoriteSide
## <chr> <chr> <chr> <chr> <chr> <dbl> <chr>
## 1 Female 25 Elsewhere beef brisket Other 3 hush puppies
## 2 Female 18 Elsewhere pulled pork Korean Style 4 fries
## 3 Male 180 Elsewhere pork ribs Eastern style (wit… 3 other
## 4 Female 24 Elsewhere pork ribs Korean Style 3 fries
## 5 Male 22 Piedmount pulled pork Eastern style (wit… 3 hush puppies
## # ℹ 6 more variables: RestaurantCity <chr>, RestaurantName <chr>,
## # MinutesDriving <chr>, SandwichPrice <chr>, DinnerPlatePrice <chr>,
## # RibsPrice <chr>
names(BBQData)
## [1] "Sex" "Age" "Hometown" "FavoriteMeat"
## [5] "FavoriteSauce" "Sweetness" "FavoriteSide" "RestaurantCity"
## [9] "RestaurantName" "MinutesDriving" "SandwichPrice" "DinnerPlatePrice"
## [13] "RibsPrice"
BBQData %>%
rowid_to_column(var = "CaseID") -> BBQData
dplyr::glimpse(BBQData)
## Rows: 15
## Columns: 14
## $ CaseID <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
## $ Sex <chr> "Male", "Female", "Female", "Male", "Female", "Female…
## $ Age <chr> "19", "24", "2", "50", "19", "20", "19", "23", "25", …
## $ Hometown <chr> "Eastern or Central NC", "Eastern or Central NC", "Ea…
## $ FavoriteMeat <chr> "beef brisket", "pulled pork", "pulled pork", "pulled…
## $ FavoriteSauce <chr> "Western style (with tomato)", "Eastern style (with n…
## $ Sweetness <dbl> 4, 1, 4, 3, 3, 4, 3, 3, 3, 2, 3, 4, 3, 3, 3
## $ FavoriteSide <chr> "fries", "other", "hush puppies", "fried okra", "bake…
## $ RestaurantCity <chr> "Wilmington", "Wilmington", "Angier ", "Wilson, NC", …
## $ RestaurantName <chr> "Jackson's", "Jackson's BBQ", "Stephenson’s BBQ", "Pa…
## $ MinutesDriving <chr> "20", "20", "20", "30", "0", "15", "20", "240", "30",…
## $ SandwichPrice <chr> "13", "10", "10", "15", "5", "3", "6", "15", "20", "1…
## $ DinnerPlatePrice <chr> "11", "15", "16", "20", "7", "18", "8", "20", "25", "…
## $ RibsPrice <chr> "15", "20", "20", "35", "9", "20", "10", "25", "30", …
head(BBQData, 5)
## # A tibble: 5 × 14
## CaseID Sex Age Hometown FavoriteMeat FavoriteSauce Sweetness FavoriteSide
## <int> <chr> <chr> <chr> <chr> <chr> <dbl> <chr>
## 1 1 Male 19 Eastern… beef brisket Western styl… 4 fries
## 2 2 Female 24 Eastern… pulled pork Eastern styl… 1 other
## 3 3 Female 2 Eastern… pulled pork Eastern styl… 4 hush puppies
## 4 4 Male 50 Elsewhe… pulled pork Western styl… 3 fried okra
## 5 5 Female 19 Eastern… pork ribs South Caroli… 3 baked beans
## # ℹ 6 more variables: RestaurantCity <chr>, RestaurantName <chr>,
## # MinutesDriving <chr>, SandwichPrice <chr>, DinnerPlatePrice <chr>,
## # RibsPrice <chr>
Insert your chunk of code here to create your unique id and then display your data.
In this section you need to covert variables into the numerical
format and clean up any messy observations. The numerical variables you
need address in this section are: Age
,
MinutesDirving
, SandwichPrice
,
DinnerPlatePrice
, and RibsPrice
.
BBQData$Age2 <- as.numeric(BBQData$Age)
BBQData$Age2[BBQData$Age2<17]<-NA
print(BBQData$Age)
## [1] "19" "24" "2" "50" "19" "20" "19" "23" "25" "49" "25" "18"
## [13] "180" "24" "22"
print(BBQData$Age2)
## [1] 19 24 NA 50 19 20 19 23 25 49 25 18 180 24 22
You know the following things about your data that will be helpful when conducting your recodes. First, all the respondents should be between the ages of 18 and 90. Second, no respondent is willing to drive more than 100 miles for BBQ. Third, it is unreasonable that the price of a sandwich is less than $5, the price of a dinner plate less than $15, or the price of ribs is less than $20. Fourth, no one should be willing to pay more than $50 dollars for a sandwich, dinner, or ribs.
After you have reformatted the variables and done the necessary recodes, use the print command to compare the original variables to your new variables.
Remember > means greater than and < means less than.
Insert your code here for recoding and cleaning numerical variables. I recommend doing one thing per chunk of code.
In this section you need to recode categorical variables to assign
values to their different categories. The following are the categorical
variables in the data set: Sex
, FavoriteMeat
,
FavoriteSauce
, FavoriteSide
.
BBQData %>%
mutate(gender2 = NA) %>%
mutate(gender2 = replace(gender2, BBQData$Sex == "Male", 1))%>%
mutate(gender2 = replace(gender2, BBQData$Sex == "Female", 2))%>%
mutate(gender2 = replace(gender2, BBQData$Sex == "Other", 3)) -> BBQData
print(BBQData$Sex)
## [1] "Male" "Female" "Female" "Male" "Female" "Female" "Other" "Male"
## [9] "Female" "Female" "Female" "Female" "Male" "Female" "Male"
print(BBQData$gender2)
## [1] 1 2 2 1 2 2 3 1 2 2 2 2 1 2 1
You should reference the code book for the BBQ data set to know the numerical values to assign to the different categories.
After you have completed recoding the variables, use the print command to compare the original variables to your new variables. Don’t forget capitalization matters.
Insert your code here for recoding and cleaning categorical variables. I recommend doing one thing per chunk of code.
Jacob Stockton
i helped no one
Click the “Knit” button to publish your work as an html document. This document or file will appear in the folder specified by your working directory. You will need to upload both this RMarkdown file and the html file it produces to AsU Learn to get all of the lab points for this week.