Quantcast

remove levels from a factor

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

remove levels from a factor

Yuan Jian
Hi,
 
how to remove levels that have less than a specific number such as 2. i.e..
 
> f<-as.factor(c("a","b","a"))
> f
[1] a b a
Levels: a b

I want to remove level b because level b has less than 2.
> f
[1] a a
Levels: a

 


     
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: remove levels from a factor

milton ruser
Hi Yuan,

It is not ellegant, but may work for you..


f<-as.factor(c("a","b","a"))

f.freq<-data.frame(table(f))
f.freq

lower.freq<-2
f.freq.subset<-subset(f.freq,f.freq$Freq>=lower.freq)
f.freq.subset

f.selected<-f[f %in% f.freq.subset$f]

f.selected<-factor(f.selected)
f.selected

Best wishes,

miltinho astronauta
brazil

On 8/29/08, Yuan Jian <[hidden email]> wrote:

>
> Hi,
>
> how to remove levels that have less than a specific number such as 2. i.e..
>
> > f<-as.factor(c("a","b","a"))
> > f
> [1] a b a
> Levels: a b
>
> I want to remove level b because level b has less than 2.
> > f
> [1] a a
> Levels: a
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: remove levels from a factor

dusadrian
In reply to this post by Yuan Jian

Yuan Jian <jayuan2008 <at> yahoo.com> writes:

> [...snip...]
>
> I want to remove level b because level b has less than 2.
> > f
> [1] a a
> Levels: a


> f[which(f %in% names(table(f))[table(f) >= 2]), drop=TRUE]
[1] a a
Levels: a

HTH,
Adrian

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: remove levels from a factor

dusadrian
Adrian Dusa <dusa.adrian <at> gmail.com> writes:
> > [...snip...]
> > f[which(f %in% names(table(f))[table(f) >= 2]), drop=TRUE]
> [1] a a
> Levels: a

Or, more simple:

> f[f %in% names(table(f))[table(f) >= 2], drop=TRUE]
[1] a a
Levels: a

Adrian

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: remove levels from a factor

Frank Harrell
Adrian Dusa wrote:

> Adrian Dusa <dusa.adrian <at> gmail.com> writes:
>>> [...snip...]
>>> f[which(f %in% names(table(f))[table(f) >= 2]), drop=TRUE]
>> [1] a a
>> Levels: a
>
> Or, more simple:
>
>> f[f %in% names(table(f))[table(f) >= 2], drop=TRUE]
> [1] a a
> Levels: a
>
> Adrian

Also see the combine.levels function in the Hmisc package.
Frank

>
> ______________________________________________
> [hidden email] 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.
>


--
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
[hidden email] 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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Loading...