[R] extract data for specific levels factor

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Oct 25 22:49:09 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Sarah Goslee
> Sent: Tuesday, October 25, 2011 12:50 PM
> To: Andrés Aragón
> Cc: R-help at r-project.org
> Subject: Re: [R] extract data for specific levels factor
> 
> Hi,
> 
> 2011/10/25 Andrés Aragón <armandres at gmail.com>:
> > Dear all,
> >
> > I'm trying to analyze data with the following structure:
> >
> > ind          cat            tx      age
> > 40.2 por fol peq     vh        35
> > 41.9 por fol med     vh        35
> > 68.9 por fol preov   vh       35
> > 71.5 por fol peq      ser       37
> > 67.5  por fol med    ser       37
> > 76.9  por fol preov   ser     37
> > 78.7  por fol peq      otr      37
> > 78.3  por fol med    otr       37
> > 82.1  por fol preov   otr      37
> > 83.9  por fol peq      vh       37
> > 80.6  por fol med      vh      37
> > 76.1  por fol preov     vh     37
> > 86.9  por fol peq        ser     35
> > 97.7  por fol med       ser     35
> > 62.3  por fol preov     ser     35
> >
> >
> >
> > I want to separate exclusively some of factor levels  (“por fol peq”
> > in the “cat” colum). I am using ggplot2  and I only can plot all of
> > factors, not separately. I did try ddply without success.
> > Any help is welcome.
> 
> This kind of question is *so* much easier to answer if you provide
> reproducible data with dput(). This is untested because of lack of
> data, but what about
> 
> newdata <- origdata[origdata$cat %in% c("por", "fol", "peq"),]
> 
> Depending on the effect you want, you may also wish to drop the
> unused levels or reorder the levels using factor().
> 
> Sarah
> 

Or, if I have guessed correctly, something like this will work

newdata<- origdata[origdata$cat == 'por fol peq',]


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204



More information about the R-help mailing list