Home > other >  split dataframe into multiple dataframes at certain row string
split dataframe into multiple dataframes at certain row string

Time:08-12

I have a dataframe that looks like this:

df1 <- data.frame(study_unit=c("region", "unit1", "unit2", "unit3", "region", "unit1", "unit2", "unit3", "region", "unit1", "unit2", "unit3", "region", "unit1", "unit2", "unit3"), values=runif(16))

I would like to have it split into four dfs everytime the string "region" appears in the 'study_unit' column, so that I get four dfs like this (numbers in the value column are irrelevant):

df_out <- data.frame(study_unit=c("region", "unit1", "unit2", "unit3"),
                  values=runif(4))

I saw this answer but when I apply it I only get the first split of the starting df.

Thanks!

CodePudding user response:

One possible solution:

split(df1, data.table::rowid(df1$study_unit))

$`1`
  study_unit    values
1     region 0.6239955
2      unit1 0.4724881
3      unit2 0.4948328
4      unit3 0.5984489

$`2`
  study_unit     values
5     region 0.83188607
6      unit1 0.37852566
7      unit2 0.50887452
8      unit3 0.01452218

$`3`
   study_unit    values
9      region 0.1818853
10      unit1 0.9160952
11      unit2 0.8608485
12      unit3 0.3201158

$`4`
   study_unit    values
13     region 0.5167159
14      unit1 0.6481682
15      unit2 0.0156648
16      unit3 0.4136461

CodePudding user response:

A second possible solution:

split(df1, cumsum(df1$study_unit == 'region'))
  • Related