0

I would like to reorder rows in a dataframe based on a specific order. Here is a dummy dataframe (in the long format) that pretty much looks like my data:

library(ggplot2)
library(dplyr)

#data frame
MV<-c(rnorm(50,mean=10, sd=1),rnorm(50,mean=9, sd=1))
ML<-c(rnorm(50,mean=12, sd=1),rnorm(50,mean=10, sd=1))
NL<-c(rnorm(50,mean=10, sd=1),rnorm(50,mean=8,sd=1))
ID<-rep(1:50,1)
Type<-rep(c("BM","NBM"),times=1, each=50)
df<-data.frame(ID, Type, MV, ML, NL)

#Here is the dataframe:
df.gat<-gather(df, "Tests", "Value", 3:5)

My data is already in the long format to start with (df.gat). The code before that is just to get you a similar dataframe.

Basically, I'd like to have my data ordered in my dataframe in the following order: NL, MV, and ML

I have tried various methods such as the following Reorder rows using custom order or How does one reorder columns in a data frame? which are not very convenient considering the number of rows in my dataset.

The solution also needs to work if some participants didn't do all the tests.

Any solution?

JKFY13
  • 77
  • 9

3 Answers3

2

In that case you could just tweak what I suggested above into:

df.gat[rev(order(df.gat$Tests)),]

which happens to do the trick for me here but not necessarily generically. If you want something generic you could (re-)create (the/a) factor:

df.gat$tests2 <- factor(df.gat$Tests, levels=c(c('NL','MV','ML')))
df.gat[order(df.gat$tests2),]

which should give you the same ordering as above.

  • I think the `factor` approach is best - you wouldn't have to explicitly add another column if not required though, e.g.: - `df.gat[order(factor(df.gat$Tests, c("NL","MV","ML"))), ]` – thelatemail Jun 09 '20 at 21:46
0

If I am understanding you correctly, you simply want to reorder the columns of your dataframe. Why not do something like this:

library(ggplot2)
library(dplyr)

#data frame
MV<-c(rnorm(50,mean=10, sd=1),rnorm(50,mean=9, sd=1))
ML<-c(rnorm(50,mean=12, sd=1),rnorm(50,mean=10, sd=1))
NL<-c(rnorm(50,mean=10, sd=1),rnorm(50,mean=8,sd=1))
ID<-rep(1:50,1)
Type<-rep(c("BM","NBM"),times=1, each=50)
df<-data.frame(ID, Type, MV, ML, NL)
df.gat<-gather(df, "Tests", "Value", 3:5)

df <- df %>% select(NL, MV, ML, everything())
ra_learns
  • 51
  • 6
  • Actually, my data is already in the long format (e.g.: df.gat). The code provided before was just to build something similar. – JKFY13 Jun 09 '20 at 18:34
0

Did you try just placing multiple parameters in an order() call like so?

df[order(MV,ML,NL),]

Your df isn't the best demonstration of this as they're all decimals. Here is a simpler alternative example using discrete values:

df2 <- data.frame(
   C1=sample(rep(c(10,20,30)   ,20)), 
   C2=sample(rep(c('A','B','C'),20)))
df2[order(df2$C1,df2$C2),]

I'm not sure why you'd need dplyr:gather() in your example if you're just working on reordering df, right?

  • I should have specified that my dataframe is in the long format to start with (df.gat). The code provided before are just to get a dataframe similar to the one I'm using. – JKFY13 Jun 09 '20 at 19:06