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.
Instructions associated with this assignment can be found in the file “InstructionsGettingtoKnowYourData.html”.
The data set you will use is different than the one used in the instructions. Pay attention to the differences in the Excel files 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.
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.
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.
install.packages(“dplyr”) install.packages(“tidyverse”) library(“dplyr”)
library(“tidyverse”) BBQ_Assignment <-
read_excel(“BBQ_Assignment.xlsx”) View(BBQ_Assignment)
Display the first 15 observations of your data set.
head(BBQ_Assignment,15) # 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.
Insert your chunk of code here to create your unique id and then
displace your data. BBQ_Assignment %>% rowid_to_column(var =
“CaseID”) -> BBQ_Assignment head(BBQ_Assignment,15) # 5. Recode and
clean numerical variables 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.
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. BBQ_Assignment\(Age2 <- as.numeric(BBQ_Assignment\)Age)
print(BBQ_Assignment\(Age)
BBQ_Assignment\)Age2[BBQ_Assignment\(Age2<17]<-NA
print(BBQ_Assignment\)Age2) BBQ_Assignment\(MinutesDriving <-
as.numeric(BBQ_Assignment\)MinutesDriving)
print(BBQ_Assignment\(MinutesDriving)
BBQ_Assignment\)MinutesDriving[BBQ_Assignment\(MinutesDriving>100]<-NA
print(BBQ_Assignment\)MinutesDriving) BBQ_Assignment\(SandwichPrice <-
as.numeric(BBQ_Assignment\)SandwichPrice)
print(BBQ_Assignment\(SandwichPrice)
BBQ_Assignment\)SandwichPrice[BBQ_Assignment\(SandwichPrice<5]<-NA
print(BBQ_Assignment\)SandwichPrice) BBQ_Assignment\(DinnerPlatePrice <-
as.numeric(BBQ_Assignment\)DinnerPlatePrice)
print(BBQ_Assignment\(DinnerPlatePrice)
BBQ_Assignment\)SandwichPrice[BBQ_Assignment\(SandwichPrice<15]<-NA
print(BBQ_Assignment\)DinnerPlatePrice) BBQ_Assignment\(RibsPrice <-
as.numeric(BBQ_Assignment\)RibsPrice) print(BBQ_Assignment\(RibsPrice)
BBQ_Assignment\)RibsPrice[BBQ_Assignment\(SandwichPrice<20]<-NA
print(BBQ_Assignment\)RibsPrice) # 6. Recode and clean
categorical variables 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.
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. BBQ_Assignment %>% mutate(FavoriteMeat2 = NA) %>% mutate(FavoriteMeat2 = replace(FavoriteMeat2, BBQ_Assignment\(FavoriteMeat == "beef brisket", 1))%>% mutate(FavoriteMeat2 = replace(FavoriteMeat2, BBQ_Assignment\)FavoriteMeat == “pulled pork”, 2))%>% mutate(FavoriteMeat2 = replace(FavoriteMeat2, BBQ_Assignment\(FavoriteMeat == "pork ribs", 3)) -> BBQ_Assignment print(BBQ_Assignment\)FavoriteMeat) print(BBQ_Assignment\(FavoriteMeat2) BBQ_Assignment %>% mutate(FavoriteSauce2 = NA) %>% mutate(FavoriteSauce2 = replace(FavoriteSauce2, BBQ_Assignment\)FavoriteSauce == “Western style (with tomato)”, 1))%>% mutate(FavoriteSauce2 = replace(FavoriteSauce2, BBQ_Assignment\(FavoriteSauce == "South Carolina Mustard", 2))%>% mutate(FavoriteSauce2 = replace(FavoriteSauce2, BBQ_Assignment\)FavoriteSauce == “Korean Style”, 3))%>% mutate(FavoriteSauce2 = replace(FavoriteSauce2, BBQ_Assignment\(FavoriteSauce == "Kansas style (with molasses)", 4))%>% mutate(FavoriteSauce2 = replace(FavoriteSauce2, BBQ_Assignment\)FavoriteSauce == “Eastern style (with no tomato)”, 5))%>% mutate(FavoriteSauce2 = replace(FavoriteSauce2, BBQ_Assignment\(FavoriteSauce == "Other", 6)) -> BBQ_Assignment print(BBQ_Assignment\)FavoriteSauce) print(BBQ_Assignment\(FavoriteSauce2) BBQ_Assignment %>% mutate(FavoriteSide2 = NA) %>% mutate(FavoriteSide2 = replace(FavoriteSide2, BBQ_Assignment\)FavoriteSide == “fries”, 1))%>% mutate(FavoriteSide2 = replace(FavoriteSide2, BBQ_Assignment\(FavoriteSide == "hush puppies", 2))%>% mutate(FavoriteSide2 = replace(FavoriteSide2, BBQ_Assignment\)FavoriteSide == “fried okra”, 3)) -> BBQ_Assignment mutate(FavoriteSide2 = replace(FavoriteSide2, BBQ_Assignment\(FavoriteSide == "baked beans", 4)) -> BBQ_Assignment mutate(FavoriteSide2 = replace(FavoriteSide2, BBQ_Assignment\)FavoriteSide == “coleslaw”, 5) -> BBQ_Assignment mutate(FavoriteSide2 = replace(FavoriteSide2, BBQ_Assignment\(FavoriteSide == "other", 6) -> BBQ_Assignment print(BBQ_Assignment\)FavoriteSide) print(BBQ_Assignment\(FavoriteSide2) BBQ_Assignment %>% mutate(gender2 = NA) %>% mutate(gender2 = replace(gender2, BBQ_Assignment\)Sex == “Male”, 1))%>% mutate(gender2 = replace(gender2, BBQ_Assignment\(Sex == "Female", 2))%>% mutate(gender2 = replace(gender2, BBQ_Assignment\)Sex == “Other”, 3)) -> BBQ_Assignment print(BBQ_Assignment\(Sex) print(BBQ_Assignment\)gender2) # Did you recieve help? I recieved help from the TA Brendan Meckler and Dr.Ondercin
No one helped me on this assignment.
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.