Order data frame by two columns in R

I’m trying to reorder the rows of a data frame by two factors. For the first factor i’m happy with the default ordering. For the second factor i’d like to impose my own custom order to the rows. Here’s some dummy data:

dat <- data.frame(apple=rep(LETTERS[1:10], 3), 
                  orange=c(rep("agg", 10), rep("org", 10), rep("fut", 10)),
                  pear=rnorm(30, 10), 
                  grape=rnorm(30, 10))

I’d like to order “apple” in a specific way:

appleOrdered <- c("E", "D", "J", "A", "F", "G", "I", "B", "H", "C")

I’ve tried this:

dat <- dat[with(dat, order(orange, rep(appleOrdered, 3))), ]

But it seems to put “apple” into a random order. Any suggestions? Thanks.

Reorder one data.frame using two columns from another data.frame in R

I have two data.frames in R, one of which has two columns and of the other of each has three columns, and where two columns are common between the two frames. The frame have the same number of rows. A

R: comparing two character columns in a big data frame

So, I have s really huge data frame, which has two columns of characters. the characters are some ID values separated by ;. So, I want to calculate the number of common ID values between these two c

Creating data.frame column with elements depending on two other columns (in R)

Before, I asked this question: Moving certain elements of data frame into new columns (in R) Now, I have the same data frame: > jiz <- data.frame(Type=c(X,B,B,B,B,X,B), + Action=c

Drop Columns R Data frame

I have a number of columns that I would like to drop from a data frame. I know that we can drop them using something like: df$x <- NULL but I was hoping to do this with fewer commands. Also, I kno

Drop Columns R Data frame

I have a number of columns that I would like to drop from a data frame. I know that we can drop them using something like: df$x <- NULL but I was hoping to do this with fewer commands. Also, I kno

Drop Columns R Data frame

I have a number of columns that I would like to drop from a data frame. I know that we can drop them using something like: df$x <- NULL but I was hoping to do this with fewer commands. Also, I kno

Sum by condition in two columns data frame R [duplicate]

This question already has an answer here: Conditional summing (R) 5 answers I have dataframe like this date apps long 10/22/2013 23:51 A 2 10/22/2013 23:52 B 3 10/22/2013 23:52 C 1 10/23/2013

Removing columns from data frame using R [duplicate]

This question already has an answer here: Drop Columns R Data frame 13 answers I am trying to remove columns, from data frame say a, of whose names are present in array, say x. a <- data.fra

R: two data frame merge

I want to merge two data frame, but there are some row names repeated. If the numbers of row name in two data frame are different, I hope the it can show NA in the less one column. My example: test

R – fast way to count data frame lines with two columns set to specific values

I have a data.frame tr_id_cat made of two columns: id, category. tr_id_cat has 15 million lines id has 300 thousand unique values category has 20 unique values I would like to build a count matrix:

Answers

Try using a factor with the levels in the desired order and the arrange function from plyr:

dat$apple <- factor(dat$apple,levels=appleOrdered)
arrange(dat,orange,apple)

Reordering the factor levels:

dat[with(dat, order(orange, as.integer(factor(apple, appleOrdered)))), ]