Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Data Science Applications in Banking
    Data Science Applications in Banking Machine Learning
  • Top Data Modeling Tools for 2023
    Top Data Modeling Tools for 2023 Machine Learning
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • learn Hadoop for Data Science
    Learn Hadoop for Data Science Machine Learning
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • How to Create an Interaction Plot in R
    How to Create an Interaction Plot in R? R
droplevels in R with examples

droplevels in R with examples

Posted on June 9June 8 By Jim No Comments on droplevels in R with examples
Tweet
Share
Share
Pin

droplevels in R with examples, To remove unneeded factor levels, use R’s droplevels() function.

This function comes in handy when we need to get rid of factor levels that are no longer in use as a result of subsetting a vector or a data frame.

The syntax for this function is as follows

droplevels(x)

where x is an object from which unused factor levels should be removed.

Count Observations by Group in R – Data Science Tutorials

This article shows you how to utilize this function in practice with a couple of examples.

Example 1: Drop Unused Factor Levels in a Vector

Assume we have a data vector with seven-factor levels. Let’s say we create a new data vector using only five of the original seven-factor levels.

define data on a seven-factor scale

data <- factor(c(1, 2, 3, 4, 5,6,7))

original data minus 4th and 5th-factor levels = new data

new <- data[-c(4, 5)]

Now we can view the new data

new
[1] 1 2 3 6 7
Levels: 1 2 3 4 5 6 7

Despite the fact that the new data only has five factors, we can see that the original seven-factor levels are still present.

How to perform the Kruskal-Wallis test in R? – Data Science Tutorials

We may use the droplevels() function to remove these unneeded factor levels.

remove any levels of factors that are no longer in use.

new <- droplevels(new)

Let’s view the data

new
[1] 1 2 3 6 7
Levels: 1 2 3 6 7

There are now only five-factor levels in the new data.

Example 2: Unused Factor Levels in a Data Frame Should Be Removed

Assume we’re working with a data frame in which one of the variables is a five-level factor.

Let’s say we create a new data frame that excludes two of these factor levels.

Checking Missing Values in R – Data Science Tutorials

Let’s create a data frame

df <- data.frame(region=factor(c('P1', 'P2', 'P3', 'P4', 'P5')),
                 sales = c(103, 106, 202, 257, 324))
df
   region sales
1     P1   103
2     P2   106
3     P3   202
4     P4   257
5     P5   324

Now we can define a new data frame

newdf <- subset(df, sales < 225)

view new data frame

newdf
    region sales
1     P1   103
2     P2   106
3     P3   202

Let’s check the levels of the region variable.

How to add labels at the end of each line in ggplot2? (datasciencetut.com)

levels(newdf$region)
[1] "P1" "P2" "P3" "P4" "P5"

The original five-factor levels are still there in the new data frame, despite the fact that the region column only has three factors.

If we tried to make any graphs with this data, we’d run into some issues.

The droplevels() function can be used to eliminate the unnecessary factor levels from the region variable:

Remove any unused factor levels.

newdf$region <- droplevels(newdf$region)

Let’s check now levels of the region variable.

How to make a rounded corner bar plot in R? – Data Science Tutorials

levels(newdf$region)
[1] "P1" "P2" "P3"

Hurray! Done for the day.

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:droplevels

Post navigation

Previous Post: Cumulative Sum calculation in R
Next Post: Augmented Dickey-Fuller Test in R

Related Posts

  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • Crosstab calculation in R
    Crosstab calculation in R R
  • Detecting and Dealing with Outliers
    Detecting and Dealing with Outliers: First Step R
  • How to change the column positions in R?
    How to change the column positions in R? R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme