[R] how to select columns

Marc Schwartz marc_schwartz at me.com
Mon Jan 30 19:44:16 CET 2012


On Jan 30, 2012, at 12:33 PM, David Winsemius wrote:

> 
> On Jan 30, 2012, at 2:30 AM, David Studer wrote:
> 
>> Hello,
>> I have the following question:
>> 
>> when creating a data.frame
>> a1<-c(1,2,3)
>> a2<-c(1,2,3)
>> c<-data.frame(a1,a2)
>> I can select columns using an index like:
>> c[,1:2]
>> Is this possible too when using column-names? (something like c(,"a1":"a2"),
>> which doesn't work):
> 
> Generally you need to use grep to convert column names to numbers for use within "[" operations]
> 
> df[ , grep("^a1$", names(df)):grep"^"a2$", names(df)) ]
> 
> -- 
> Another David


Just to throw out another option here, the ?subset function has a 'select' argument, which supports a start:end syntax to extract sequential columns from a data frame. Thus:

  subset(DF, StartColumnName:EndColumnName)

gets you that ability. The column names are NOT quoted, so in your case:

  subset(DF, select = a1:a2)

You can even select sequential and non-sequential columns by using c() along with the start:end syntax:

  subset(DF, select = c(ColA, ColF:ColH, ColK, ColN:ColW, ColZ))

HTH,

Marc Schwartz

> 
>> 
>> Alternative question: Is there a function to get the index of a variable by
>> name
> 
> That's what grep will do.
> 
>> or can I
>> select certain columns using a loop? (a_1, a_2, ..., a_n)
>> 
>> Thank you very much!
>> David
> 
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
\



More information about the R-help mailing list