2

I have the vector

x <- c("A", "B", "C", "D", "E", "F")

that I split in the following manner:

split(x, 1:2)

It comes out as (a, c, e) and (b, d, f), yet I want (a, b, c) and (d, e, f). Any way of changing it to a horizontal split rather than a vertical one?

Konrad
  • 17,740
  • 16
  • 106
  • 167
Katrina
  • 51
  • 3
  • Have a look at [this discussion](http://stackoverflow.com/questions/3318333/split-a-vector-into-chunks-in-r) that covers numerous ways of splitting vectors. – Konrad Dec 07 '15 at 17:08
  • [This function](https://gist.github.com/sckott/4632735) also could be useful. – Konrad Dec 07 '15 at 17:12

2 Answers2

5

You can do:

split(x, rep(1:2, each = length(x)/2))

which gives:

$`1`
[1] "A" "B" "C"

$`2`
[1] "D" "E" "F"
maccruiskeen
  • 2,748
  • 2
  • 13
  • 23
2

We can also use gl

split(x, as.numeric(gl(length(x), 3, length(x))))
akrun
  • 874,273
  • 37
  • 540
  • 662