In R, How to sum over the current row in a data frame

I have df (A) (ncol = 1, nrow = 1356)

col1 5 7 9 3 2 3.8 24 2.7 12 11 23 .... to 1356 row... 

I need a sum from the first line to the fifth line, and then from the second line to the seventh line, etc. In addition, each value is rolling for a fraction of the number of rows. Expected results, for example:

 5*1/5 + 7*2/5 + 9*3/5 + 3*4/5 + 2*5/5= (result) 7*1/5 + 9*2/5 + 3*3/5 + 2*4/5 + 3.8*5/5= (result) 9*1/5 + 3*2/5 + 2*3/5 + 3.8*4/5 + 24*5/5= (result) and so on for the 1356 row 

In the synthesis, the sum is every 5 lines, collapses the data line by line. Thank you in advance.

-2
source share
2 answers

We could use shift from data.table

 library(data.table) m1 <- na.omit(do.call(cbind, shift(df1$col1, 0:4, type="lead"))) rowSums(m1*(1:5)[col(m1)]/5) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 

Or another option

  m1 <- embed(df1$col1,5) rowSums(m1*(5:1)[col(m1)]/5) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 
0
source

I would use my favorite function R, filter :

 DF <- read.table(text = "col1 5 7 9 3 2 3.8 24 2.7 12 11 23", header = TRUE) 5*1/5 + 7*2/5 + 9*3/5 + 3*4/5 + 2*5/5 #[1] 13.6 7*1/5 + 9*2/5 + 3*3/5 + 2*4/5 + 3.8*5/5 #[1] 12.2 9*1/5 + 3*2/5 + 2*3/5 + 3.8*4/5 + 24*5/5 #[1] 31.24 c(na.omit(stats::filter(DF$col1, (5:1)/5))) #[1] 13.60 12.20 31.24 25.58 30.48 32.58 44.88 
+2
source

All Articles