[R] extract data for specific levels factor

Dennis Murphy djmuser at gmail.com
Tue Oct 25 23:32:09 CEST 2011


Are you trying to separate the substrings in cat? If so, one way is to
use the colsplit() function in the reshape2 package, something like
(untested since you did not provide a suitable data format with which
to work):

library('reshape2')
splitcat <- colsplit(mydata$cat, ' ', names = c('fat', 'bat', 'rat'))
moredat <- cbind(mydata, splitcat)

Other options that might pertain to your request include:

(i) subset(mydata, cat == 'por fol pec')
which you can use as a data argument inside ggplot2 - e.g.,

ggplot(subset(mydata, cat == 'por fol pec'), aes(x = age, y = ind)) +
   geom_point()

(ii) use faceting to get individual plots by factor level of cat - e.g.,
ggplot(mydata, aes(x = age, y = ind)) +
   geom_point() +
   facet_grid( ~ cat)

Hope that one of these is close to the bullseye...
Dennis


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.
>
> Andrés
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list