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?
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
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