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
Post a Comment