简体   繁体   中英

How to select columns with partial match to sort them

I have a dataframe df . I need to reorder the columns together by putting _case columns together, _control columns together, MCI together and unknown together. I can do this by using grepl and then cbind the columns in this order, but is there an efficient way to do this?

df <- structure(list(Ethnicity = structure(1:4, .Label = c("AA", "Asian", 
"Hispanic", "NHW"), class = "factor"), `<65_case` = c(29L, 13L, 
63L, 1215L), `<65_control` = c(0L, 0L, 0L, 0L), `<65_MCI` = c(75L, 
23L, 43L, 385L), `<65_unknown` = c(6L, 5L, 19L, 126L), `<70_case` = c(45L, 
25L, 109L, 2321L), `<70_control` = c(0L, 0L, 0L, 1L), `<70_MCI` = c(124L, 
36L, 76L, 633L), `<70_unknown` = c(14L, 6L, 23L, 166L), `<75_case` = c(73L, 
47L, 167L, 3704L), `<75_control` = c(0L, 0L, 0L, 1L), `<75_MCI` = c(174L, 
52L, 103L, 863L), `<75_unknown` = c(29L, 7L, 33L, 220L)), row.names = c(NA, 
-4L), class = "data.frame")

Does this work:

> library(dplyr)    
> df %>% select(ends_with('case'),ends_with('control'),ends_with('MCI'),ends_with('unknown'))
  <65_case <70_case <75_case <65_control <70_control <75_control <65_MCI <70_MCI <75_MCI <65_unknown <70_unknown <75_unknown
1       29       45       73           0           0           0      75     124     174           6          14          29
2       13       25       47           0           0           0      23      36      52           5           6           7
3       63      109      167           0           0           0      43      76     103          19          23          33
4     1215     2321     3704           0           1           1     385     633     863         126         166         220
> 

Using regex:

> df[-1][order(gsub('(.*)_(.*)','\\2',colnames(df)[-1]))]
  <65_case <70_case <75_case <65_control <70_control <75_control <65_MCI <70_MCI <75_MCI <65_unknown <70_unknown <75_unknown
1       29       45       73           0           0           0      75     124     174           6          14          29
2       13       25       47           0           0           0      23      36      52           5           6           7
3       63      109      167           0           0           0      43      76     103          19          23          33
4     1215     2321     3704           0           1           1     385     633     863         126         166         220
> 
require(tidyverse)

df %>% select(contains("case"), contains("control"), contains("MCI"), contains("unknown"))

Like this?

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