If you only have Col1
as non-numeric, you could use:
df[,c("Col1",as.character(sort(as.numeric(names(df)[-1]),decreasing=F)))]
Col1 11 12 110 1100
1 a 1 22 2 20
2 b 16 18 5 3
Otherwise:
To add to @DavidArenburg 's comment, R will almost always add an X to numeric column names. The trouble is that even if you removed this X, the presence of col1 makes it hard to sort these names. dplyr
's select
in combination with everything
might make it easier as we use below.
df<-read.table(text="Col1 11 110 1100 12
a 1 2 20 22
b 16 5 3 18",header=T)
names(df) <- gsub("X","",names(df))
As @akrun points out, we can skip the gsub
by setting check.names=FALSE
in read.table
i.e:
df<-read.table(text="Col1 11 110 1100 12
a 1 2 20 22
b 16 5 3 18",header=T, check.names= FALSE)
Proceeding with dplyr
:
library(dplyr)
df %>%
select(Col1,`11`,`12`,everything())
Col1 11 12 110 1100
1 a 1 22 2 20
2 b 16 18 5 3