There is an interesting option drop = TRUE
in data.frame filtering, see excerpt from help('[.data.frame')
:
Usage
S3 method for class 'data.frame'
x[i, j, drop = ]
But when I try it on data.frame, it doesn't work!
> df = data.frame(a = c("europe", "asia", "oceania"), b = c(1, 2, 3))
>
> df[1:2,, drop = TRUE]$a
[1] europe asia
Levels: asia europe oceania <--- oceania shouldn't be here!!
>
I know there are other ways like
df2 <- droplevels(df[1:2,])
but the documentation promised much more elegant way to do this, so why it doesn't work? Is it a bug? Because I don't understand how this could be a feature...
EDIT: I was confused by drop = TRUE
dropping factor levels for vectors, as you can see here. It is not very intuitive that [i, drop = TRUE]
drops factor levels and [i, j, drop = TRUE]
does not!!