krtbris
krtbris

Reputation: 368

Is there an easier way to re-order columns in R when I have >1000 columns?

At the moment, I am using the code below to reorder my columns in a data frame in R:

df_2000 <- df_2000[,c(1:7, 1222:1293, 8:1221)]

The problem with this is, if my data frame changes at all, then the code is rendered useless and has to change each time.

What I want is all my variables that begin with "compid" at the end of my data frame and all the other variables with different names before the compid variables. E.G. minimized version:

df_2000 now:

pheno1 pheno2 compid1 compid2 compid3 pheno3 pheno4

How I want df_2000 ordered:

pheno1 pheno2 pheno3 pheno4 compid1 compid2 compid3 

THANKS

Upvotes: 0

Views: 56

Answers (2)

user2974951
user2974951

Reputation: 10375

df=data.frame(matrix(ncol=7))
colnames(df)=c("pheno1", "pheno2", "compid1", "compid2", "compid3", "pheno3", "pheno4")

cbind(
  df[,!grepl("compid",colnames(df))],
  df[,grepl("compid",colnames(df))]
)

  pheno1 pheno2 pheno3 pheno4 compid1 compid2 compid3
1     NA     NA     NA     NA      NA      NA      NA

A more efficient way

df[,c(colnames(df)[!grepl("compid",colnames(df))],colnames(df)[grepl("compid",colnames(df))])]

Upvotes: 1

dspn
dspn

Reputation: 314

names <- c("pheno1", "pheno2", "compid1", "compid2", "compid3", "pheno3", "pheno4")
x <- c(sort(names[substring(names, 1, 3) == "com"]), sort(names[substring(names, 1, 3) == "phe"]))

df_2000 <- df_2000[,x]

might be problematic if you have variables like "compid1" instead of "compid001"

Upvotes: 0

Related Questions