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 in Data Analytics by ravi
46 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 by Cherukuri
• 32,260 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 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 in Data Analytics by payal
60 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,560 points
3,733 views
0 votes
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 55 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 in Data Analytics by Cherukuri
• 32,260 points
492 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
124 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
173 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
89 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
113 views
0 votes
2 answers
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 27 in Data Analytics by anonymous
• 32,260 points
37 views