select() set position

后端 未结 2 985
南笙
南笙 2021-01-27 23:41

If I have df like this

df <- read.table(text=\"
              id date       paid_at    binded_at  
1            107 2016-12-16 2017-06-02 2017-06-07
2                 


        
相关标签:
2条回答
  • 2021-01-27 23:49
    df %>% 
      select(id, paid_at, everything())
    

    You can just select id and paid_at, and then add everything() to select all other columns as well in their original order.

    0 讨论(0)
  • 2021-01-28 00:14

    We can hardcode first three columns and then select the remaining ones with setdiff.

    cols <- c("paid_at", "id", "binded_at")
    df[c(cols, setdiff(names(df), cols))]
    
    #     paid_at  id  binded_at       date
    #1 2017-06-02 107 2017-06-07 2016-12-16
    #2 2017-06-02 107 2017-06-07 2017-11-27
    #3 2017-06-02 107 2017-06-07 2017-11-28
    #4 2017-01-01 109 2017-06-07 2016-11-28
    #5 2017-01-01 109 2017-06-07 2017-11-29
    #6 2018-01-01 110 2017-06-07 2017-12-04
    

    With select it can be similarly done

    library(dplyr)
    select(df, c(cols, setdiff(names(df), cols)))
    
    0 讨论(0)
提交回复
热议问题