Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R sum consecutive duplicate odd rows and remove all but first

Tags:

r

I am stuck with question - how to sum consecutive duplicate odd rows and remove all but first row. I have got how to sum consecutive duplicate rows and remove all but first row (link: https://stackoverflow.com/a/32588960/11323232). But this project, i would like to sum the consecutive duplicate odd rows but not all of the consecutive duplicate rows.

 ia<-c(1,1,2,NA,2,1,1,1,1,2,1,2)
 time<-c(4.5,2.4,3.6,1.5,1.2,4.9,6.4,4.4, 4.7, 7.3,2.3, 4.3)
 a<-as.data.frame(cbind(ia, time))

  a
   ia time
1   1  4.5
2   1  2.4
3   2  3.6
5   2  1.2
6   1  4.9
7   1  6.4
8   1  4.4
9   1  4.7
10  2  7.3
11  1  2.3
12  2  4.3

to 

 a
   ia time
1   1  6.9
3   2  3.6
5   2  1.2
6   1  20.4
10  2  7.3
11  1  2.3
12  2  4.3

how to edit the following code for my goal to sum consecutive duplicate odd rows and remove all but first row ?

 result <- a %>%
 filter(na.locf(ia) == na.locf(ia, fromLast = TRUE)) %>%
 mutate(ia = na.locf(ia)) %>%
 mutate(change = ia != lag(ia, default = FALSE)) %>%
 group_by(group = cumsum(change), ia) %>%
 # this part
 summarise(time = sum(time))
like image 484
hees Avatar asked Nov 19 '25 02:11

hees


1 Answers

One dplyr possibility could be:

a %>%
 group_by(grp = with(rle(ia), rep(seq_along(lengths), lengths))) %>%
 mutate(grp2 = ia %/% 2 == 0,
        time = sum(time)) %>%
 filter(!grp2 | (grp2 & row_number() == 1)) %>%
 ungroup() %>%
 select(-grp, -grp2)

      ia  time
  <dbl> <dbl>
1     1   6.9
2     2   3.6
3     2   1.2
4     1  20.4
5     2   7.3
6     1   2.3
7     2   4.3
like image 122
tmfmnk Avatar answered Nov 21 '25 16:11

tmfmnk