Try this:

subset(x, select = grep("cru", names(x)))

On Wed, Jun 9, 2010 at 2:41 PM, Josh B <joshb41@yahoo.com> wrote:

> Hello R listserve,
>
> I would appreciate someone's help with this problem. Consider the following
> toy dataset:
>
> x <- read.table(textConnection("worldclim.1 worldclim.2 cru.1 cru.2
> indv.1 7 8 32 658
> indv.2 7 7 39 422"), header = TRUE)
>
> How could I create a subset of the data based on the column prefix? For
> instance, let's say I wanted to subset only the columns with the "cru"
> prefix. Keep in mind that my real data set is very large and likely to
> change through time, so simply subseting by column index -- e.g., x2 <-
> subset(x[,3:4]) -- would not be a good strategy.
>
> Specific code that will give me the answer would be most valuable. I tried
> looking up ?rm and ?ls, but I was not able to understand the syntax.
>
> Thanks very much for your help!
> Josh
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]

