-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
  • 411
  • 1
  • 3
  • 14
  • 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 <- data.frame(ID = 1:11, col1 = c(10, 40, 5, 33, 37, 2, 5, 102, 11, 76,9)); split_points <- c(which(df$col1 < 10), nrow(df)+1); last_p <- 1; indices <- lapply(split_points, function(p){ if(last_p >= p-1){ tmp <- numeric(0) }else{ tmp <- seq(last_p, p-1) } last_p <<- p+1; tmp }); lapply(indices, function(idx){ df[idx, ,drop=FALSE] })` – 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
  • 254,981
  • 17
  • 203
  • 341