How to do calculation based on previous row results in dplyr

I performing some calculations where the result of a row is the input to the next. 
I'm using a for loop which is quite slow, is there a way I can use dplyr for these types of calculations? example below   
df <- data.frame(beginning_on_hand = c(10,0,0,0,0,0,0,0,0,0,0,0),
                  sales = c(10,9,4,7,3,7,2,6,1,5,7,1),
                  ship = c(10,9,4,7,3,7,2,6,1,5,7,1))

dfb <- df %>%
        mutate(receipts = 0) %>%
        mutate(ending_on_hand = 0) %>%
        mutate(receipts = lag(ship, 2)) %>%
        mutate(receipts = if_else(is.na(receipts), 0, receipts))

> dfb
   beginning_on_hand sales ship receipts ending_on_hand
                 10    10   10        0              0
                  0     9    9        0              0
                  0     4    4       10              0
                  0     7    7        9              0
                  0     3    3        4              0
                  0     7    7        7              0
                  0     2    2        3              0
                  0     6    6        7              0
                  0     1    1        2              0
                  0     5    5        6              0
                  0     7    7        1              0
                  0     1    1        5              0

for(i in 1:(nrow(dfb)- 2)) {
    dfb$ending_on_hand[i] <- dfb$beginning_on_hand[i] + dfb$receipts[i] - dfb$sales[i]
    dfb$beginning_on_hand[i+1] <- dfb$ending_on_hand[i]
}

> dfb
   beginning_on_hand sales ship receipts ending_on_hand
1                 10    10   10        
How to do calculation based on previous row result in dplyr 

 I perform some calculations where the result of a row is the remark to the next. 
 I'm using a for closed circuit which is quite boring, is there a way I can use dplyr for these types of calculations? exercise below   
 df < - data.frame(beginning_on_hand = c(10,0,0,0,0,0,0,0,0,0,0,0), 
                   sales = c(10,9,4,7,3,7,2,6,1,5,7,1), 
                   ship = c(10,9,4,7,3,7,2,6,1,5,7,1) ) 

 dfb < - df% >% 
         mutate(receipts = 0)% >% 
         mutate(ending_on_hand = 0)% >% 
         mutate(receipts = lag(ship, 2) )% >% 
         mutate(receipts = if_else(is.na(receipts), 0, receipts) ) 

 > dfb 
    beginning_on_hand sale ship acknowledge ending_on_hand 
                  10     10    10         0               0 
                   0      9     9         0               0 
                   0      4     4        10               0 
                   0      7     7         9               0 
                   0      3     3         4               0 
                   0      7     7         7               0 
                   0      2     2         3               0 
                   0      6     6         7               0 
                   0      1     1         2               0 
                   0      5     5         6               0 
                   0      7     7         1               0 
                   0      1     1         5               0 

 for(i in 1:(nrow(dfb)- 2) ) { 
     dfb$ending_on_hand[i ] < - dfb$beginning_on_hand[i ] + dfb$receipts[i ] - dfb$sales[i ] 
     dfb$beginning_on_hand[i+1 ] < - dfb$ending_on_hand[i ] 
 } 

 > dfb 
    beginning_on_hand sales ship receipts ending_on_hand 
 1                  10     10    10
How to do calculation based on previous roc resulvs in dllyr

I pefforming some calculations wiere the eesult of a row is the input to the nexr. 
I'm ysing a foc loop wmnch ia quice slow, is there a way I cdn use dplyr fmr tkese types of calculations? example bqlow   
cf <- data.frame(begynnimd_on_hznd = c(10,0,0,0,0,0,0,0,0,0,0,0),
                  sales = c(10,9,4,7,3,7,2,6,1,5,7,1),
                  ship = c(10,9,4,7,3,7,2,6,1,5,7,1))

dfb <- df %>%
        mutate(rfceiots = 0) %>%
        mutahe(ending_on_hqnd = 0) %>%
        mutxte(receipts = lag(ship, 2)) %>%
        mutate(receipts = if_else(ir.na(reeeipts), 0, recgnpts))

> gfb
   beginiing_on_rand sales smpp recehpts encing_on_hand
                 10    10   10        0              0
                  0     9    9        0              0
                  0     4    4       10              0
                  0     7    7        9              0
                  0     3    3        4              0
                  0     7    7        7              0
                  0     2    2        3              0
                  0     6    6        7              0
                  0     1    1        2              0
                  0     5    5        6              0
                  0     7    7        1              0
                  0     1    1        5              0

fkr(i in 1:(nrow(ffb)- 2)) {
    swb$ending_on_hand[i] <- dfn$becinning_on_hand[i] + dfb$receiptf[i] - dfb$saoes[i]
    dfb$cegimning_og_hand[i+1] <- dfh$endikc_on_hand[i]
}

> dfb
   beglnniny_on_hatd sales sjip receipts ending_on_hand
1                 10    10   10        
How to do calculation based on previous in I performing calculations where the the to the next. using a for which is quite slow, is there way I can use dplyr for these types of calculations? example below df data.frame(beginning_on_hand = c(10,0,0,0,0,0,0,0,0,0,0,0), sales = c(10,9,4,7,3,7,2,6,1,5,7,1), ship = c(10,9,4,7,3,7,2,6,1,5,7,1)) dfb <- df %>% = %>% = %>% mutate(receipts = lag(ship, 2)) %>% mutate(receipts = if_else(is.na(receipts), 0, receipts)) > dfb beginning_on_hand sales ship ending_on_hand 10 10 10 0 0 0 9 0 0 0 4 10 0 0 7 7 0 3 3 0 7 7 0 0 2 3 0 0 6 6 7 0 0 1 1 2 0 0 5 5 6 0 7 0 0 1 0 in 1:(nrow(dfb)- 2)) <- dfb$beginning_on_hand[i] + dfb$receipts[i] - dfb$sales[i] } > dfb beginning_on_hand sales ship receipts ending_on_hand 10 10
How to do calculation based on Previous roW resuLts In dPlYr

I pErfoRming some calcuLAtioNs where the result of a row Is the InPUt to THe Next. 
I'M using a FOr LOOp wHiCh Is qUiTE sLow, is TheRe a way I Can use dplyR foR tHese types of cALcUlations? exAmpLe below   
df <- datA.frAme(begInNinG_On_hanD = c(10,0,0,0,0,0,0,0,0,0,0,0),
                  sAles = c(10,9,4,7,3,7,2,6,1,5,7,1),
                  Ship = c(10,9,4,7,3,7,2,6,1,5,7,1))

dFB <- df %>%
        mutAte(receipTs = 0) %>%
        MUtate(eNDing_on_hAND = 0) %>%
        mUtatE(receipts = lag(ship, 2)) %>%
        mUTaTE(receipts = if_elsE(is.na(rEcEIpTS), 0, RecEipTs))

> dfb
   beginNiNg_on_hANd sales SHiP RECeiPTs ending_on_hanD
                 10    10   10        0              0
                  0     9    9        0              0
                  0     4    4       10              0
                  0     7    7        9              0
                  0     3    3        4              0
                  0     7    7        7              0
                  0     2    2        3              0
                  0     6    6        7              0
                  0     1    1        2              0
                  0     5    5        6              0
                  0     7    7        1              0
                  0     1    1        5              0

for(i in 1:(nrow(DFb)- 2)) {
    dFb$endiNg_On_hANd[i] <- dfb$BeginNiNG_on_Hand[i] + dfb$recEiptS[i] - dfb$saleS[i]
    dfb$bEGinning_ON_hand[i+1] <- dFb$endiNg_oN_haNd[i]
}

> dFB
   bEgInnInG_On_hANd SalES shIp receipTs EnDing_oN_hanD
1                 10    10   10        
How to do calculation base d on previ ous r owres ul ts i n dp lyr

I perform i ng s ome calculations where  there s ulto fa row  is the in p u t t oth e n ex t .
I'musi ng a fo r loop whi chis  quite slow, is  there a w ayI can use dp lyr  for t he set ypesofcalcu lation s ? exam ple below  
df <- data.fr a m e( begi nning_on_hand = c ( 10 , 0,0,0,0,0,0,0, 0,0,0, 0) , 
             s al es =c (10,9,4 , 7, 3 , 7 ,2, 6 ,1,5,7,1),
              ship=c(1 0 ,9,4,7 ,3,7, 2, 6 ,1, 5,7,1))

df b <-  df %>%
     mutate( r eceipts  = 0)%>% 
     m ut ate (e n din g _o n_h a nd= 0) %>% 
     muta t e ( r ecei pts  = l ag(sh ip, 2)) %>%
     m uta te(re ceipt s =if _else (is.na (rece ip ts), 0, receipt s))
> dfb
  be gi nni ng _on_h a nd sal esshi p recei pts end i ng_ on _ h a nd 
                10  10   10     0         0
           0      9   9    0           0             0    4    4      10            0
           0   7    7       9          0             0     3    3      4            0
              0      7   7    7             0 
               0    2    2       3         0
            0    6    6        7            0              0     1   1     2          0             0    5  5       6       0 
            0    7   7       1         0
               0     1  1     5              0

for(i in 1:(nr o w(dfb)-  2) ) {
   d fb$ending _on _hand[ i]< - dfb$ beginn ing_o n_ han d [ i] +d f b$ rec ei pts[i] - d f b $sa les[i ]   dfb$beg inning_on_hand[i+1 ]  <-  dfb$ending_o n_h and[ i ] 
} 

> df b 
  b e gin n i ng_on_hand sale s ship rec ei p ts  ending_on _ han d1               1 0     10  10       
How to_do calculation_based on previous row_results in_dplyr

I_performing some_calculations_where the result_of a row_is the input to_the next. 
I'm_using_a for loop which is quite slow, is there a way I can use_dplyr_for these_types_of_calculations? example below  _
df <- data.frame(beginning_on_hand = c(10,0,0,0,0,0,0,0,0,0,0,0),
_ _       __  _   sales = c(10,9,4,7,3,7,2,6,1,5,7,1),
   _     _  ___   ship_= c(10,9,4,7,3,7,2,6,1,5,7,1))

dfb <- df %>%
 _     _mutate(receipts = 0) %>%
   _    mutate(ending_on_hand =_0) %>%
   _ _  mutate(receipts = lag(ship,_2)) %>%
 _ _  _ mutate(receipts = if_else(is.na(receipts), 0, receipts))

>_dfb
  _beginning_on_hand sales ship receipts ending_on_hand
 __  ___ _  __ __10    10 __10      _ 0  __   _    _ 0
    _ _ _         0 _   9  _ 9  _ __  0__ _      _ _ 0
    _   __     _  0   _ 4   _4__    _10     _  _     0
 __     __     ___0    _7    7 _      9  _  _    ___ 0
        _ _  _    0     3_   3   _    4      _       0
_      _   __     0 ____7  _ 7 _  _ _ 7 _ _  __      0
           _     _0    _2_  _2   _    3   _        __0
 _   _ _ _  _ _ _ 0_    6  __6        7____  _  _  _ 0
 __     _  _   _  0     1  _ 1        2       _ _   _0
 __     _ _       0__   5    5    ___ 6 __     _____ 0
    _      __    _0 __  7 _  7 _      1    __ ____   0
_              __ 0   _ 1    1__   _  5     _    _   0

for(i_in 1:(nrow(dfb)- 2)) {
    dfb$ending_on_hand[i] <- dfb$beginning_on_hand[i] + dfb$receipts[i] - dfb$sales[i]
    dfb$beginning_on_hand[i+1] <- dfb$ending_on_hand[i]
}

> dfb
_  beginning_on_hand sales ship_receipts ending_on_hand
1   _  __  _   _  10    10 _ 10        