Search code examples
rdatabase

Reversing the order of column names that contain both numbers and letters in R


I have a data set (df) with many columns, the names of which contain letters and numbers. For example

pr.2013 pr.2012 pr. 2011 cd.2013 cd.2012 cd.2011 and so on and I want to order them alphabetically to this format

cd.2013 cd.2012 cd.2011 pr.2013 pr.2012 pr. 2011

I tried

df1<-df %>% select(order(colnames(.))) 

but with no success. Is there any way to do that in R. Manually is not an option as I have a large number of similar columns.

Many thanks in advance!


Solution

  • If this is your data

      pr.2012 pr.2011 pr.2013 cd.2012 cd.2013 cd.2011
    1       1       2       3       4       5       6
    2       2       3       4       5       6       7
    3       3       4       5       6       7       8
    

    Using the strsplit by . and arranged colnames to select the variables in the right order.

    library(dplyr)
    
    df[,match(data.frame(t(data.frame(strsplit(colnames(df), "\\.")))) %>% 
        arrange(X1, desc(X2)) %>% 
        reframe(paste0(X1, ".", X2)) %>% 
        unlist(use.names=F), colnames(df))]
      cd.2013 cd.2012 cd.2011 pr.2013 pr.2012 pr.2011
    1       5       4       6       3       1       2
    2       6       5       7       4       2       3
    3       7       6       8       5       3       4
    
    Data
    df <- structure(list(pr.2012 = 1:3, pr.2011 = 2:4, pr.2013 = 3:5, cd.2012 =
     4:6, cd.2013 = 5:7, cd.2011 = 6:8), class = "data.frame", row.names =
     c(NA, -3L))