简体   繁体   中英

R - keep only columns with column names that match a string

I am relatively new to R and I've had a lot of luck finding answers on here, but this one has me stumped after 2 days of trying things.

I have a dataframe with column names like this:

TargetID sample1.beta sample1.avg sample1.error sample1.pval sample2.beta sample2.avg sample2.error sample2.pval

This repeats for thousands of samples. I need to create multiple separate data frames for each piece of data: one for beta, one for avg, one for error, one for pval. I also need to keep the 1st column with the TargetID in all data frames. The resulting data frames would have column names like:

TargetID sample1.beta sample2.beta sample3.beta

TargetID sample1.pval sample2.pval sample3.pval

etc.

I have found answers for subsetting data frames but they don't seem to apply to selecting all columns that contain a specific string (and keeping the 1st column).

I've also been exploring whether this is better done with the txt file before I import into R with awk.

Use grepl or grep in the second position of "[" with a pattern that includes TargetID and the subset string applied to names(dfrm_name) :

 avg_sub <- dfrm[ , grepl( "^TargetID|avg$", names(dfrm) ]

The "^" pattern matches the beginning of a string, while the "$" pattern matches the end of a string.

You can try (as you don't supply example data using mtcars ):

library(dplyr)
# select the column mpg and all the columns containing an r
head(mtcars %>% select(mpg, contains("r")))
                   mpg drat gear carb
Mazda RX4         21.0 3.90    4    4
Mazda RX4 Wag     21.0 3.90    4    4
Datsun 710        22.8 3.85    4    1
Hornet 4 Drive    21.4 3.08    3    1
Hornet Sportabout 18.7 3.15    3    2
Valiant           18.1 2.76    3    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM