Working with sums in R -


how 1 work sums in r? can't seem find easy way calculate sums \sum_{i=m}^n a_i. there 3 things decide here; summation starts, ends, , elements summed.

i have data frame df , calculate sum_{i=1}^{n-3} df$col[i]*df$col[i+3], col being column of length 1000 in df, i.e. n=1000... how can this? i've found 1 cumbersome way of doing it, namely

new = null  (n in 1:997) { new = df$col[n]*df$col[n+3] }  sum(new) 

that's stupid way of doing it, how in more "natural" way? yes, i'm sure precise question has been asked didn't know how narrow down searches. "r+sum+why dont programmers think mathematicians", maybe ;) anyway, hints or links tutorials r beginners appreciated, thanks.

you can with:

sum(df$col[1:997] * df$col[4:1000]) 

this deal quicker looping through indices , individually multiplying.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -