Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is split always internally sorting the resulting data frames

Tags:

r

I just wondering if I can be sure that split() is always sorting the results? What is the rule for the sorting? The example works, but I haven't found the according 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 remain the order which is supplied?

like image 546
Sebastian Avatar asked Nov 03 '22 22:11

Sebastian


1 Answers

The sort order of split is the factor order of your grouping variable. So, if your grouping variable is a factor, then the levels of that factor are kept intact.

An example: modify your data so that fac has the 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 of split are in the same order as the levels of 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
like image 137
Andrie Avatar answered Nov 07 '22 21:11

Andrie