JKFY13
JKFY13

Reputation: 77

Reordering rows in a dataframe in r

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?

Upvotes: 0

Views: 1664

Answers (3)

D. Anthropoid
D. Anthropoid

Reputation: 143

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.

Upvotes: 3

D. Anthropoid
D. Anthropoid

Reputation: 143

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?

Upvotes: 0

ra_learns
ra_learns

Reputation: 113

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())

Upvotes: 0

Related Questions