-3

Is there an easy way in R (i.e. without using a for loop) to break a data frame up based on a column that cycles through the same N+1 numbers z times?

So here's an example:

column1: 0, 1, 2, 3, ..., N, 0, 1, 2, 3, ..., N, ......, 0, 1, 2, 3, ..., N
column2: (randomly distributed numbers of same length as column1)

I want **z** of data frames that look like the following:

Df1:

column11: 0, 1, 2, 3, ..., N
column21: (randomly distributed numbers of same length as column1a)
.
.
.

Dfz:

column1**z**: 0, 1, 2, 3, ..., N
column2**z**: (randomly distributed numbers of same length as column1z)

Also, when I say I want **z** data frames, I really just want a way to use the first N+1 data points, the 2nd N+1 data points, and so on.

Jaap
  • 81,064
  • 34
  • 182
  • 193
eTothEipiPlus1
  • 577
  • 2
  • 9
  • 28
  • 2
    Maybe something like `dd<-data.frame(a=rep(1:5, 10), b=runif(50)); do.call(cbind, split(dd, rep(1:10, each=5)))` it would be much nicer if you have a description of your input/output in terms of actual code or objects rather than word problems that could possibly be misinterpreted in order to make your question clear and [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Sep 11 '14 at 20:09
  • I'm not sure what you mean by not clear. I have 2 columns in the data frame. One column is a set of ordered integers starting at 1 and ending at some integer N (you can say that N=100 if you'd like but that shouldn't matter). Then column1 repeats say z times. I want z separate data frames rather than one large data frame. Is that clear enough? – eTothEipiPlus1 Sep 11 '14 at 21:16
  • 1
    If you could modify MrFlicks example using just `split(dd, rep(1:10, each=5))` without all the `do.call` part. That will create 10 data frames within a list. There is an option to export them all into an environment using `list2env`, but polluting environment with numerous objects is very not recommended. The best way is to leave them in the list format and operate on them using functions like `lapply` – David Arenburg Sep 11 '14 at 22:10
  • Okay, well thanks everyone. I'm not sure why I got so many down votes though. Is the answer to my question obvious or something? – eTothEipiPlus1 Sep 12 '14 at 11:39

1 Answers1

0
split( dfrm , cumsum( c(0, diff(dfrm$col1)) < 0) )

split returns a list of objects, in this case data.frames, separated by the unique values of the second argument. The cumsum strategy increments by 1 with each encounter of a "drop" in col1 value..

Tested on:

N <- 5
dfrm <- data.frame( col1 = rep(0:N, 5) , col2 = runif(30) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487