Moving df variables starting with the same prefix to other positions

Hi, I have this simple df:

source <- data.frame(
   stringsAsFactors = FALSE,
                URN = c("21GB01097000",
                        "21G170101345","21GB00097827"),
               QA31 = c("aaa", "bbb", "ccc"),
  TMC.Communication = c(0, 1, 0),
                QA1 = c(3, 1, 7),
      TMC.Paperwork = c(0, 0, 0),
          TMC.Other = c(1, 0, 1)
)

I know I can move the most interesting variables first using this:

result <- source %>% 
    select(URN, TMC.Communication, TMC.Paperwork, 
         TMC.Other, everything())

but can I replace all variables starting with TMC by something more general? I would like to avoid listing all TMC variables in the select statement...

library(dplyr)
source %>% relocate(starts_with("TMC"),.after="URN")

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.