Always split, internally sorting the resulting data frames

I'm just wondering, can I be sure that split () always sorts the results? What is the rule for sorting? The example works, but I did not find the corresponding line in the help pages. Sorry if I misread the help.

 dat.exmpl <- data.frame(cbind(a=11:20, b=rep(3,10)), fac = c(2,1))
 split(dat.exmpl, dat.exmpl$fac)
 dat.exmpl <- data.frame(cbind(a=11:20, b=rep(3,10)), fac=rep(c("blueb","bluea")))
 split(dat.exmpl, dat.exmpl$fac)

ExtraQ: is there a way to leave an order that ships?

+5
source share
1 answer

Sort order splitis the factor order of your grouping variable. So, if your grouping variable is a factor, then the levels of this factor remain untouched.

Example: change your data to fachave levels c("blueb","bluea"):

dat.exmpl <- data.frame(
  a=11:20, 
  b=rep(3,10), 
  fac=factor(rep(c("blueb","bluea")), levels=c("blueb","bluea"))
)

Then the results splitare in the same order as the levels fac:

split(dat.exmpl, dat.exmpl$fac)
$blueb
   a b   fac
1 11 3 blueb
3 13 3 blueb
5 15 3 blueb
7 17 3 blueb
9 19 3 blueb

$bluea
    a b   fac
2  12 3 bluea
4  14 3 bluea
6  16 3 bluea
8  18 3 bluea
10 20 3 bluea
+2
source

All Articles