[R] Sub-setting a data frame by partial column names?
Sarah Henderson
sarah.b.henderson at gmail.com
Thu Mar 4 08:40:45 CET 2010
Hi Jim, and thanks for your solution. I figured one out for myself
about a minute after I posted this, and here it is if anyone else can
find it valuable:
subsetData<- bigData[,grep(partialName, colnames(bigData))]
This is smaller than your solution, but similar I think.
Cheers,
Sarah
On Thu, Mar 4, 2010 at 6:34 PM, Jim Lemon <jim at bitwrit.com.au> wrote:
> On 03/04/2010 05:51 PM, Sarah Henderson wrote:
>>
>> Hi all --
>>
>> I think my Python brain is missing something crucial about string
>> operations in R, but I cannot figure this out.
>>
>> I have a large data frame with several groups of similar variables.
>> Similar variables are named according to their group, and I am now
>> writing a function to check correlations within groups. I want to
>> subset the data frame by partial variable name, something along the
>> lines of this:
>>
>> partialName<- "XXX"
>> subsetData<- bigData[, partialName in colnames(bigData)]
>>
>> Where bigData might have 10 columns named "XXX1", "XXX2" etc.
>>
>
> Hi Sarah,
>
> Try this:
>
> column.names<-paste(sample(c("X","Y","Z"),100,TRUE),
> sample(c("X","Y","Z"),100,TRUE),
> sample(c("X","Y","Z"),100,TRUE),
> sample(0:9,100,TRUE),sep="")
> column.names[grep("XXX",column.names,fixed=TRUE)]
>
> Jim
>
More information about the R-help
mailing list