Change the default order of labels of factor in R

0 votes

Hi, I want to rate the feedback from user of options given below as 1 - 4. Can someone help?

Feedback Good OK Can be Better Very Bad
Rating 1 2 3 4
Jun 27, 2019 in Data Analytics by ravi
3,048 views

2 answers to this question.

0 votes

Hi Ravi,

You can change the levels in a factor by using factor() and mention explicitly the order of levels in a vector.

Use statement like below,

feedback = c("Good","OK","Can be Better","Very Bad")

order = factor(feedback, levels = c("Good","OK","Can be Better", "Very Bad"))

print(order)

Hope it helps!

answered Jun 27, 2019 by Cherukuri
• 33,030 points
0 votes

You can also use relevel() to change the level in a factor.

relevel() takes the factor and the level element and push it to front in the level.

write as below - 

feedback = c("Good","OK","Can be Better","Very Bad")
rating = factor(feedback)
print(rating)
print(as.integer(rating))
rating = relevel(rating,"Can be Better")
rating = relevel(rating,"OK")
rating = relevel(rating,"Good")
print(rating)
print(as.integer(rating))

answered Jun 27, 2019 by sindhu

Related Questions In Data Analytics

0 votes
1 answer

Change the order of legend values in a plot in R

@prardhana, Use scale_fill/color/size_discrete/contin....(labels = c()). compare both to see ...READ MORE

answered Nov 4, 2019 in Data Analytics by payal
1,996 views
0 votes
1 answer

Changing the order of bars in a bar-plot - ggplot2 - R

You can use the scale_x_discrete() function with ...READ MORE

answered May 28, 2018 in Data Analytics by Bharani
• 4,660 points
9,738 views
+1 vote
1 answer

Is it possible to change the column names of the dataset that I'm using in R?

That is definitely possible in R. You ...READ MORE

answered Dec 17, 2018 in Data Analytics by Anisha

edited Dec 17, 2018 1,746 views
0 votes
1 answer

Change the order of multiple legends in ggplot

Hi Radha, You can change the order of ...READ MORE

answered Jul 24, 2019 in Data Analytics by Cherukuri
• 33,030 points
25,143 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,265 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,962 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 1,200 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,393 views
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,866 views
0 votes
1 answer

How to change the datatype of any field in an external dataset in R?

Hi Anand, Try as below - class(dataset/dataframe$col_name) <- ...READ MORE

answered Jun 28, 2019 in Data Analytics by anonymous
• 33,030 points
789 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP