Order data frame by two columns in R -


i'm trying reorder rows of data frame 2 factors. first factor i'm happy default ordering. second factor i'd impose own custom order rows. here's 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 order "apple" in 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 seems put "apple" random order. suggestions? thanks.

reordering factor levels:

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

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -