Question: Suppose 1 , 2 , 3 stands for KFC , Subway and McDonald respectively. Use factor ( ) to turn the numeric vector c (

Suppose 1,2,3 stands for KFC, Subway and McDonald respectively. Use factor() to turn the numeric
vector c(2,1,2,1,3,3) into a factor vector named food.
b) Now confirm that food is indeed a factor using str(), which reveals the structure of an R object.
c) Now please change the first element in food from 2 to 3
Hint: food[6] will gives the 6th element in food.
d) Function levels() can produce the labels of the factors. Please correct the all the errors in the code
below, which tries to change the labels into McDonald, Subway and KFC.
level(food)= c(KFC, Subway, McDonald)
e) After fixing the codes in the previous sub-question, please use summary() or table() to check the
frequency of McDonald, KFC, and Subway in food.
f) Use plot() to obtain a bar chart of the variable food. Why do you think the bars are sorted on the
x-axis this way?
g) There are many ways to re-order the levels in a factor but here we will focus on the following two
options here.
relevel() to specify the reference level, i.e the category that is first on the list. This has a
particular implication in a regression setting.
Use the levels argument in factor()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related General Management Questions!