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]]
Try this: DF[,grep("BOUTLENGTHTOT", l)] On Thu, Jan 14, 2010 at 4:43 PM, Martin Striz <mstriz at gmail.com> 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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