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,250 points
791 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,380 points
944 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 946 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,750 points
2,623 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,403 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,661 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,350 points
1,099 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,920 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,937 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
1,100 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