[R] choosing multiple columns

Ista Zahn istazahn at gmail.com
Sat Aug 11 14:46:24 CEST 2012


Hi Sachin,

There are at least two ways. The safer way is to use a regular
expression to find the matching columns, like this:

a <- initial_data[grep("^OFB[0-9]+", names(initial_data))]

Alternatively, if you know that the columns you want are the first 8
you can select them by position, like this:

a <- initial_data[1:8]

Best,
Ista

On Sat, Aug 11, 2012 at 7:59 AM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
> Hi all,
>
> I have a data frame that has the columns OFB1, OFB2, OFB3,... OFB10.
>
> How do I select the first 8 columns efficiently without typing each and
> every one of them. i.e. I want something like:
>
> a<-data.frame(initial_data$OFB1-10) #i know this is wrong, what would be
> the correct syntax?
>
> Thanks,
> Sachin
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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