[R] levels of factor when subsetting the factor

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Sep 12 19:23:46 CEST 2006

```Yes. I do this periodically:

dat.new <- dat[1:6, ]
dat.new[] <- lapply(dat.new, function(x)
if(is.factor(x)) factor(x) else x)

HTH,

--sundar

Afshartous, David said the following on 9/12/2006 11:00 AM:
> thanks to all for the quick replies!
>
> if the factor is part of a dataframe, I can apply the subsetting
> to the entire dataframe, and then use drop=True to the factor
> separately and then put it back into the new dataframe (code below).  is there a way
> to do this in a single step?
>
> dat <-data.frame(fact = as.factor(c(rep("A", 3),rep("B", 3), rep("C", 3))),Y = rnorm(9))
> dat.new = dat[1:6, ]
> dat.new\$fact = dat\$fact[1:6, drop = T]
>
>
>
>
> -----Original Message-----
> From: pd at pubhealth.ku.dk [mailto:pd at pubhealth.ku.dk] On Behalf Of Peter Dalgaard
> Sent: Tuesday, September 12, 2006 11:45 AM
> To: Afshartous, David
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] levels of factor when subsetting the factor
>
> "Afshartous, David" <afshart at exchange.sba.miami.edu> writes:
>
>>
>> All,
>>
>> When I take a subset of a factor the reduced factor still maintains
>> all the original levels of the factor when say forming the key in a plot.
>> The data is correct, but the variable still "remembers" the original
>> levels.  See below for reproducible code.  Does anyone know how to fix
>> this?
>> cheers,
>> dave
>>
>> fact = as.factor(c(rep("A", 3),rep("B", 3), rep("C", 3))) new.fact =
>> fact[1:6]
>>> new.fact
>>  A A A B B B
>> Levels: A B C    ## should only show A B
>
> Just use
>
>> factor(new.fact)
>  A A A B B B
> Levels: A B
>
> or
>
>> fact[1:6, drop=T]
>  A A A B B B
> Levels: A B
>
>
> And, no, it is not a bug. The fact that a subsample happens to consist only of males does not turn gender into a one-level factor... (Apart from the philosophy, it makes a real difference in tabulation.)
>
>

```