Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order data frame by two columns in R

Tags:

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.

like image 369
Steve Avatar asked Jul 20 '11 23:07

Steve


People also ask

How do you sort a Dataframe based on two columns in R?

setorder() available in data. table library which is also used to sort the dataframe rows by multiple columns. Finally, we have use arrange() to sort the dataframe by multiple columns in ascending order and using desc() to sort it in descending from the dplyr package.

How do I order columns from a Dataframe in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.

Can we group by two columns in R?

By using group_by() function from dplyr package we can perform group by on multiple columns or variables (two or more columns) and summarise on multiple columns for aggregations.

How do you order by in R?

order() in R The numbers are ordered according to its index by using order(x) . Here the order() will sort the given numbers according to its index in the ascending order. Since number 2 is the smallest, which has an index as five and number 4 is index 1, and similarly, the process moves forward in the same pattern.


2 Answers

Reordering the factor levels:

dat[with(dat, order(orange, as.integer(factor(apple, appleOrdered)))), ]
like image 173
Charles Avatar answered Oct 02 '22 04:10

Charles


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)
like image 45
joran Avatar answered Oct 02 '22 06:10

joran