[R] extracting columns with same partial name

Peter Ehlers ehlers at ucalgary.ca
Thu Jan 14 20:08:28 CET 2010


Try

dat[names(dat) %in% paste("BOUTLENGTHTOT", 1:17, sep="")]

  -Peter Ehlers

Martin Striz wrote:
> Hi folks, I'm new to the list.
> 
> I have a data file with 256 columns.  Here's just a subset of names(data):
> 
>   [1] "MOUSE"               "BASEDATE1"           "PERCENTSLEEPTOT1"
>   [4] "PERCENTSLEEPNIGHT1"  "PERCENTSLEEPDAY1"    "BOUTLENGTHTOT1"
>   [7] "BOUTLENGTHNITE1"     "BOUTLENGTHDAY1"      "BOUTTHRESTOTP1"
>  [10] "BOUTTHRESHNITEP1"    "BOUTTHRESHDAYP1"     "BOUTTHRESTOTN1"
>  [13] "BOUTTHRESHNITEN1"    "BOUTTHRESHDAYN1"     "ACTONSET1"
>  [16] "PEAKACT1"            "BASEDATE2"           "PERCENTSLEEPTOT2"
>  [19] "PERCENTSLEEPNIGHT2"  "PERCENTSLEEPDAY2"    "BOUTLENGTHTOT2"
>  [22] "BOUTLENGTHNITE2"     "BOUTLENGTHDAY2"      "BOUTTHRESTOTP2"
>  [25] "BOUTTHRESHNITEP2"    "BOUTTHRESHDAYP2"     "BOUTTHRESTOTN2"
>  [28] "BOUTTHRESHNITEN2"    "BOUTTHRESHDAYN2"     "ACTONSET2"
>  [31] "PEAKACT2"            "BASEDATE3"           "PERCENTSLEEPTOT3"
>  [34] "PERCENTSLEEPNIGHT3"  "PERCENTSLEEPDAY3"    "BOUTLENGTHTOT3"
> 
> 
> You'll notice that certain names repeat, like BOUTLENGTHTOT1,
> BOUTLENGTHTOT2, etc.  These represent each day of data recording, and they
> go up to 17.
> 
> I want to extract, for example, BOUTLENGTHTOT1 - 17.  I'm new to R, and the
> only ways that I know how to this are:
> 
> newData <- data[, c("BOUTLENGTHTOT1", "BOUTLENGTHTOT2", ....)]
> 
> or
> 
> newData <- data.frame(col1 = data$BOUTLENGTHTOT1, col2 =
> data$BOUTLENGTHTOT2, .....)
> 
> Both of which take a long time for 17 columns.  Is there a way that I can
> use a wildcard to grab all BOUTLENGTHTOT* columns?
> 
> Thanks,
> Martin
> 
> 	[[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.
> 
> 

-- 
Peter Ehlers
University of Calgary
403.202.3921



More information about the R-help mailing list