Split data.frame into groups based on value using dplyr R

I have a big data.frame file (200000) and I need to add a column for grouping and the groups are separated by a row with a specific value, eg.

  s<-"A B C
  1 2 1
  2 22 3
  0 0 -1
  2 12 2
  0 0 -1
  20 2 5
  1 3 1
  0 2 2"
  d<-read.delim(textConnection(s),sep=" ",header=T)

      

C == - 1 is a breakpoint for each group, as a result I need 3 groups:

  require(dplyr)

      

here i find lines that separate groups

  mutate(d,rn=row_number()) %>% filter(C==-1) 

      

and then i can build data.frame i need

  bind_rows(slice(d, 1:2) %>% mutate(grp=1),slice(d,4) %>%mutate(grp=2), slice(d,6:n()) %>% mutate(grp=3)) 

      

How can I do this without hardcoding breaks?

+3


source to share


1 answer


How about this:

d %>% mutate(grp = cumsum(C == -1) + 1) %>% filter(C != -1)

      



cumsum(C == -1)

will give you the group column and all that's left is the filter.

+4


source







All Articles