-1

I have the following data frame in R:

ID  col1
1   10
2   40
3   5
4   33
5   37
6   2
7   102
8   11
9   76

I'd like to get a set of data frames from this by partitioning where the rule for the partitioning is that: split where col1<10 (and omit rows where col1<10, albeit this could be done later, of course). So the requested output:

df1:

ID  col1
1   10
2   40

df2:

ID  col1
1   33
2   37

df3:

ID  col1
1   102
2   11
3   76

Thank you for any insight.

Fredrik
  • 295
  • 1
  • 2
  • 11
  • I don't think this question is actually a duplicate of the linked question. The linked question want's to split on a factor, here there is a flexible condition `col1 < 10`. – const-ae Oct 10 '19 at 14:29
  • This code does the trick: `df = p-1){ tmp – const-ae Oct 10 '19 at 14:35

1 Answers1

1

This creates a list whose elements are the individual data frames:

grp <- cumsum(df$col1 < 10)
by(df, grp, subset, col1 >= 10)

giving:

grp: 0
  ID col1
1  1   10
2  2   40
------------------------------------------------------------ 
grp: 1
  ID col1
4  4   33
5  5   37
------------------------------------------------------------ 
grp: 2
  ID col1
7  7  102
8  8   11
9  9   76

Note

The input in reproducible form:

Lines <- "ID  col1
1   10
2   40
3   5
4   33
5   37
6   2
7   102
8   11
9   76"
df <- read.table(text = Lines, header = TRUE)
G. Grothendieck
  • 233,926
  • 16
  • 195
  • 321