Search code examples
rdataframedplyrtidyrwrangle

Split dataframe into list based on identical consecutive element


Is there an efficient way to split a dataframe based on identical consecutive element in a column into a list (and keep the order of the dataframe element inside the list) as follow ?

The dataframe :

X__1
S003
S003
S003
S006
S006
S011
S007
S007
S003
S003
S005
S006

Into :

$`1`
S003
S003
S003

$`2`
S006
S006

$`3`
S011

$`4`
S007
S007

$`5`
S003
S003

$`6`
S005

$`7`
S006

I tried to use : split(df, interaction(df$X__1)) but this would create groups by categories from my list as follow :

$`1`
S003
S003
S003
S003
S003

$`2`
S005

$`3`
S006
S006
S006

$`4`
S007
S007

$`6`
S011

Thanks for the help :)


Solution

  • We can use the rleid function from data.table to split it, i.e.

    split(df, data.table::rleid(df$X__1))