Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6052311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:54:52+00:00 2026-05-23T07:54:52+00:00

The issue of dropping unused factor levels when subsetting has come up before .

  • 0

The issue of dropping unused factor levels when subsetting has come up before. Common solutions include using character vectors where possible by declaring

options(stringsAsFactors = FALSE)

Sometimes, though, ordered factors are necessary for plotting, in which case we can use convenience functions like droplevels to create a wrapper for subset:

subsetDrop <- function(...){droplevels(subset(...))}

I realize that subsetDrop mostly solves this problem, but there are some situations where subsetting via [ is more convenient (and less typing!).

My question is how much further, for the sake of convenience, can we push this to be the ‘default’ behavior of R by overriding [ for data frames to automatically drop factor levels. For instance, the Hmisc package contains dropUnusedLevels which overrides [.factor for subsetting a single factor (which is no longer necessary, since the default [.factor appears to have a drop argument for dropping unused levels). I’m looking for a similar solution that would allow me to subset data frames using [ but automatically dropping unused factor levels (and of course preserving order in the case of ordered factors).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:54:53+00:00Added an answer on May 23, 2026 at 7:54 am

    I’d be really wary of changing the default behavior; you never know when another function you use depends on the usual default behavior. I’d instead write a similar function to your subsetDrop but for [, like

    sel <- function(x, ...) droplevels(x[...])
    

    Then

    > d <- data.frame(a=factor(LETTERS[1:5]), b=factor(letters[1:5]))
    > str(d[1:2,])
    'data.frame':   2 obs. of  2 variables:
     $ a: Factor w/ 5 levels "A","B","C","D",..: 1 2
     $ b: Factor w/ 5 levels "a","b","c","d",..: 1 2
    > str(sel(d,1:2,))
    'data.frame':   2 obs. of  2 variables:
     $ a: Factor w/ 2 levels "A","B": 1 2
     $ b: Factor w/ 2 levels "a","b": 1 2
    

    If you really want to change the default, you could do something like

    foo <- `[.data.frame`
    `[.data.frame` <- function(...) droplevels(foo(...))
    

    but make sure you know how namespaces work as this will work for anything called from the global namespace but the version in the base namespace is unchanged. Which might be a good thing, but it’s something you want to make sure you understand. After this change the output is as you’d like.

    > str(d[1:2,])
    'data.frame':   2 obs. of  2 variables:
     $ a: Factor w/ 2 levels "A","B": 1 2
     $ b: Factor w/ 2 levels "a","b": 1 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Issue: RODBC (falsely) returning zero rows Situation: I'm using RODBC to connect to a
Issue Using the following just simply doesn't work properly in -webkit- and -moz- browsers:
Issue Garbage gets erroneously entered into date fields and screws up calculations. Goal Using
I'm having an issue with temp tables using an ADO connection to my database.
Here is some context to my issue.... using iOS 5 as the target for
issue facing while loading the messages in to mq channel, recieving an error like
Issue for text elements that are adjacent to icon elements, the glyph height is
// issue with the following not returning info or just stalling... but no errors
Issue - We have a spreadsheet with thousands of addresses but no counties. Goal
Issue is SOLVED: Problem is that it is a pre-compiled web page project and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.