<- c(
y "Disagree", "Disagree", "Agree", "Agree", "Strongly Agree",
"Strongly Agree", "Strongly Disagree", "Strongly Disagree",
"Agree", "Agree", "Neutral", "Disagree",
"Strongly Disagree", "Neutral", "Strongly Agree", "Agree",
"Disagree", "Strongly Agree", "Strongly Agree", "Agree",
"Neutral", "Agree", "Disagree", "Agree", "Agree"
)
Quiz 4 Instructions
Please complete the following questions and submit a file named Quiz4.R to Gradescope for autograding.
Remember:
- Do not use global paths in you script. Instead, use setwd() interactively in the console, but do not forget to remove or comment out this part of the code before you submit. The directory structure of your machine is not the same as the one on Gradescope’s virtual machines.
- Do not destroy or overwrite any variables in your program. I check them only after I have run your entire program from start to finish.
- Check to make sure you do not have any syntax errors. Code that doesn’t run will get a very bad grade.
- Make sure to name your submission Quiz4.R
Tip: before submitting, it might help to clear all the objects from your workspace, and then source your file before you submit it. This will often uncover bugs.
Question 1
- [1pts] Let
vec1 <- seq(1, 10, 2)
. Generate a factorvec1_factor
usingvec1
.
- [1pts] Check if
vec1
is a vector? A factor? (Hint: You may useis.factor()
andis.vector()
for this purpose.) In addition, useclass()
function to check the type ofvec1
.
- [1pts] Check if
vec1_factor
is a vector? A factor? In addition, useclass()
function to check the type ofvec1_factor
.
- [1pts] Generate another factor
vec1_factor10
usingvec1
, butlevels = 1:10
. Useidentical(vec1_factor, vec1_factor10)
to compare if the two factors are the same.
Question 2
[2pts] The typical 5-point Likert scale includes the following levels:
- Strongly Disagree
- Disagree
- Neutral
- Agree
- Strongly Agree
Based on the vector y
above, please generate a ordinal factor y_factor
, which has the ordered levels as shown above (that is, ‘Strongly Disagree’ < ‘Disagree’ < ‘Neutral’ < ‘Agree’ < ‘Strongly Agree’).
Question 3
<- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
days_of_week
<- c("Saturday", "Wednesday", "Sunday", "Friday") DAYS
- [1pt] Sort the
DAYS
vector usingsort()
function. (Please note that the results are in alphabetical order rather than the order of the weekdays.)
- [1pt] Encode the vector
DAYS
into a factorDAYS_f
, by usingdays_of_week
above as the levels for the factor.
- [1pt] Sort
DAYS_f
usingsort()
function. (Please note that the results now is in the order of the weekdays.)
- [1pt] Use the function
unclass()
to obtain the integer vector associated toDAYS_f
.