Override [.data.frame to drop unused factor levels by default
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 simil开发者_开发问答ar 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).
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
you can do that work by overwriting the default value of the drop argument like this:
formals(`[.factor`)$drop <- TRUE
UPDATE
as for data.frame, you can do by:
`[.data.frame` <- function(...)droplevels(base::`[.data.frame`(...))
actually similar as @Aaron's one.
if you want to cancel this behavior, then:
rm(`[.data.frame`)
will do that.
> d <- data.frame(a=letters[1:10], b=LETTERS[1:10])
> str(d[1:5, ])
'data.frame': 5 obs. of 2 variables:
$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5
$ b: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5
> `[.data.frame` <- function(...)droplevels(base::`[.data.frame`(...))
> str(d[1:5, ])
'data.frame': 5 obs. of 2 variables:
$ a: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5
$ b: Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5
> rm(`[.data.frame`)
> str(d[1:5, ])
'data.frame': 5 obs. of 2 variables:
$ a: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5
$ b: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5
I think that changing the default is very dangerous, see my response here.
Most cases where people are concerned with dropping factor levels you either really don't need to (sumarizing something that you forced to have 1 value is silly) or there is a better way to accomplish what you are trying. The possible side effects from auto dropping is potentially worse than the couple of keystrokes saved. Also if you are doing reproducable research then you should not be depending on or even allowing the computer to change data without your specific say so.
精彩评论