1

How can I automatically (or systematically) rearrange the columns of my dataframe according to some elements contained in the name of the columns. For example,

df <- data.frame(name_001_a=letters[1:4], 
             value_003_a=c(rep(TRUE, 2), rep(FALSE, 2)), 
             other_002_a=letters[5:8])

I like to rearrange the columns according to the number in the name of the columns (001, 002, 003), so my dataframe is:

> df <- df[c(1,3,2)]
  name_001_a other_002_a value_003_a
1          a           e        TRUE
2          b           f        TRUE
3          c           g       FALSE
4          d           h       FALSE

How can I effectively do that with a large number of variables? Thxs

Rmeow
  • 113
  • 5

2 Answers2

1

Use str_extract of stringr package

library(stringr)
df[,order(str_extract(colnames(df),"[0-9]+"))]
#  name_001_a other_002_a value_003_a
#1          a           e        TRUE
#2          b           f        TRUE
#3          c           g       FALSE
#4          d           h       FALSE
d.b
  • 32,245
  • 6
  • 36
  • 77
1

Here is another option with mixedsort

library(gtools)
df[mixedsort(names(df))]
#  name_001_a other_002_a value_003_a
#1          a           e        TRUE
#2          b           f        TRUE
#3          c           g       FALSE
#4          d           h       FALSE

Or with gsub

df[order(as.integer(gsub("^[^0-9]+|[^0-9]+$", "", names(df))))]
akrun
  • 874,273
  • 37
  • 540
  • 662