[R] subset data right

William Dunlap wdunlap at tibco.com
Fri May 27 17:49:57 CEST 2016


>If you want to drop levels, use droplevels() either on the factor or on
the >subset of your data frame. Example:
>droplevels(f[1]) #One element, only one level

Calling factor() on a factor, as the OP did, also drops any unused levels,
as the examples showed.

> str(factor(factor(letters)[11:13]))
 Factor w/ 3 levels "k","l","m": 1 2 3
> str(droplevels(factor(letters)[11:13]))
 Factor w/ 3 levels "k","l","m": 1 2 3

Using droplevels instead of factor does make the intent clearer and
droplevels works on data.frames.





Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, May 27, 2016 at 3:37 AM, S Ellison <S.Ellison at lgcgroup.com> wrote:

> > You did not change df$quant - you made a new object called 'subdf'
> > containing a column called 'quant' that had only one level.  Changing
> subdf has
> > no effect on df.
>
> Also, subsetting a factor _intentionally_ does not change the number of
> levels. Example:
> f <- factor(sample(letters[1:3], 30, replace=TRUE))
> f[1]  #One element, still three levels
>
> If you want to drop levels, use droplevels() either on the factor or on
> the subset of your data frame. Example:
> droplevels(f[1]) #One element, only one level
>
>
> Also worth noting that df is a function.
>  > df <- data.frame(quant=factor(letters))
> looks very like you're assigning a data frame to the function 'df'
> (density for the F distribution)
> It doesn't, because R is clever. But it's really not good practice to use
> common function names as variable names. Too much potential for confusion.
>
> S Ellison
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:13}}



More information about the R-help mailing list