[R] subset data right

S Ellison S.Ellison at LGCGroup.com
Fri May 27 12:37:31 CEST 2016


> 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 use...{{dropped:8}}



More information about the R-help mailing list