Re: [R] factor question

From: Mark Difford <mark_difford_at_yahoo.co.uk>
Date: Thu, 24 Jul 2008 13:07:40 -0700 (PDT)

Hi Edna,

Because I am "always" subsetting, I keep the following function handy

mydata[] <- lapply(mydata, function(x) if(is.factor(x)) x[,drop=T] else x)

This will strip out all factor levels that have been dropped by a previous subsetting operation. For novice users of R (though I am not suggesting that you are) it's priceless.

The original author, as far as I know, is Andy Liaw (author of the randomForest package).

HTH, Mark.

Edna Bell wrote:
>
> Hi!
>
> Suppose I have a factor:
>

>> fac1 <- factor(rep(c("dog","cat","tree"),c(2,3,4)))
>> fac1

> [1] dog dog cat cat cat tree tree tree tree
> Levels: cat dog tree
>> length(fac1)

> [1] 9
>
> Now I want to get rid of the dogs:
>> fac1 <- fac1[3:9]
>> fac1

> [1] cat cat cat tree tree tree tree
> Levels: cat dog tree
>>

> How should I remove the dog from the level, please?
>
> Thanks in advance,
> Edna Bell
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
-- 
View this message in context: http://www.nabble.com/factor-question-tp18638814p18639010.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 24 Jul 2008 - 20:12:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Jul 2008 - 20:34:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive