The data frame consists of the variables Date
, Type
and Total
, where Type
is either Buy
or Sell
.
How can we group the observations such that only neighboring observations of the same Type
are grouped together, then sum the Total
of all observations in each group. In other words, we keep adding the next observation to the current group until the value of Type
changes.
For example, in the data frame below, the groups are as follows
- Obs
1
&2
- Obs
3
&4
- Obs
5
&6
- Obs
7
,8
&9
Reproducible data, thank you @bgoldst:
df1 <- data.frame(Date=rep(as.POSIXct('2016-06-16 06:27:39'),9L),
Type=c('Buy','Buy','Sell','Sell','Buy','Buy','Sell','Sell','Sell'),
Total=c(1.548012e+01,1.051480e+02,5.956740e+00,3.872415e+01,1.333391e+02,1.941060e-01,1.941060e-01,1.941060e-01,3.277059e-01))