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.
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.
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.
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.
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.
Reordering the factor levels:
dat[with(dat, order(orange, as.integer(factor(apple, appleOrdered)))), ]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With