Reordering levels of a factor variable

0 votes

I am working with the iris dataset and this is the normal ordering of levels i get w.r.t the Species column:

levels(iris$Species)
[1] "setosa"     "versicolor" "virginica"

I would want to change the ordering of levels to -> "virginica" "versicolor" "setosa", i.e, i want to reverse the order of levels

May 15, 2018 in Data Analytics by DataKing99
• 8,240 points
608 views

1 answer to this question.

0 votes

The factor() function provides the 'levels' attribute for this purpose:

iris$Species <- factor(iris$Species, levels =c("virginica","versicolor","setosa") ) 

All you have to do is, use the levels attribute and reorder the factors.

answered May 15, 2018 by Bharani
• 4,660 points

Related Questions In Data Analytics

0 votes
1 answer

All Levels of a Factor in a Model Matrix in R

Yes, you can modify the model.matrix() function ...READ MORE

answered Jun 22, 2023 in Data Analytics by anonymous
• 1,180 points
571 views
0 votes
1 answer

With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

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

edited Apr 3, 2018 by DeepCoder786 755 views
0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
2,324 views
0 votes
1 answer

How to drop factor levels in a subsetted data frame?

You can use factor(ff) to drop levels ...READ MORE

answered Apr 17, 2018 in Data Analytics by kappa3010
• 2,090 points

edited Apr 17, 2018 by kappa3010 5,056 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,281 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,360 points
794 views
0 votes
1 answer
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,631 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,652 views
+1 vote
2 answers

Finding number of missing values and removing those missing values from a data-frame

To find number of missing values for ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
876 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