sum of a subset of rows in a data table [duplicate]












0
















This question already has an answer here:




  • Consecutive/Rolling sums in a vector in R

    4 answers



  • Running Sum in R data.table [duplicate]

    1 answer




I have a little problem...let's say I have a data.table with one numerical column like:



  NR
1
2
3
5
7
10
1


I want to create a new column which is computed in this way:
in row j I want the sum of NR in the rows j, j+1, j+2. So I want this result:



  NR    NEW_NR
1 6
2 10
3 15
5 22
7 18
10 11
1 1


Could anyone help me pls?










share|improve this question













marked as duplicate by Jimbou, Martin Schmelzer, Community Nov 14 '18 at 14:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    have a look at shift or rolling sum functions from other packages.

    – mt1022
    Nov 14 '18 at 14:36











  • @mt1022 thank you, maybe this is what I need!

    – Francesco Tamberi
    Nov 14 '18 at 14:38











  • dt[, S := Reduce(`+`, shift(NR, 0:2, type = "lead"))]

    – Martin Schmelzer
    Nov 14 '18 at 14:39













  • @Jimbou you are right, I'll delete asap the question!

    – Francesco Tamberi
    Nov 14 '18 at 14:44
















0
















This question already has an answer here:




  • Consecutive/Rolling sums in a vector in R

    4 answers



  • Running Sum in R data.table [duplicate]

    1 answer




I have a little problem...let's say I have a data.table with one numerical column like:



  NR
1
2
3
5
7
10
1


I want to create a new column which is computed in this way:
in row j I want the sum of NR in the rows j, j+1, j+2. So I want this result:



  NR    NEW_NR
1 6
2 10
3 15
5 22
7 18
10 11
1 1


Could anyone help me pls?










share|improve this question













marked as duplicate by Jimbou, Martin Schmelzer, Community Nov 14 '18 at 14:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    have a look at shift or rolling sum functions from other packages.

    – mt1022
    Nov 14 '18 at 14:36











  • @mt1022 thank you, maybe this is what I need!

    – Francesco Tamberi
    Nov 14 '18 at 14:38











  • dt[, S := Reduce(`+`, shift(NR, 0:2, type = "lead"))]

    – Martin Schmelzer
    Nov 14 '18 at 14:39













  • @Jimbou you are right, I'll delete asap the question!

    – Francesco Tamberi
    Nov 14 '18 at 14:44














0












0








0









This question already has an answer here:




  • Consecutive/Rolling sums in a vector in R

    4 answers



  • Running Sum in R data.table [duplicate]

    1 answer




I have a little problem...let's say I have a data.table with one numerical column like:



  NR
1
2
3
5
7
10
1


I want to create a new column which is computed in this way:
in row j I want the sum of NR in the rows j, j+1, j+2. So I want this result:



  NR    NEW_NR
1 6
2 10
3 15
5 22
7 18
10 11
1 1


Could anyone help me pls?










share|improve this question















This question already has an answer here:




  • Consecutive/Rolling sums in a vector in R

    4 answers



  • Running Sum in R data.table [duplicate]

    1 answer




I have a little problem...let's say I have a data.table with one numerical column like:



  NR
1
2
3
5
7
10
1


I want to create a new column which is computed in this way:
in row j I want the sum of NR in the rows j, j+1, j+2. So I want this result:



  NR    NEW_NR
1 6
2 10
3 15
5 22
7 18
10 11
1 1


Could anyone help me pls?





This question already has an answer here:




  • Consecutive/Rolling sums in a vector in R

    4 answers



  • Running Sum in R data.table [duplicate]

    1 answer








r data.table






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 14:33









Francesco TamberiFrancesco Tamberi

6418




6418




marked as duplicate by Jimbou, Martin Schmelzer, Community Nov 14 '18 at 14:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Jimbou, Martin Schmelzer, Community Nov 14 '18 at 14:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    have a look at shift or rolling sum functions from other packages.

    – mt1022
    Nov 14 '18 at 14:36











  • @mt1022 thank you, maybe this is what I need!

    – Francesco Tamberi
    Nov 14 '18 at 14:38











  • dt[, S := Reduce(`+`, shift(NR, 0:2, type = "lead"))]

    – Martin Schmelzer
    Nov 14 '18 at 14:39













  • @Jimbou you are right, I'll delete asap the question!

    – Francesco Tamberi
    Nov 14 '18 at 14:44














  • 1





    have a look at shift or rolling sum functions from other packages.

    – mt1022
    Nov 14 '18 at 14:36











  • @mt1022 thank you, maybe this is what I need!

    – Francesco Tamberi
    Nov 14 '18 at 14:38











  • dt[, S := Reduce(`+`, shift(NR, 0:2, type = "lead"))]

    – Martin Schmelzer
    Nov 14 '18 at 14:39













  • @Jimbou you are right, I'll delete asap the question!

    – Francesco Tamberi
    Nov 14 '18 at 14:44








1




1





have a look at shift or rolling sum functions from other packages.

– mt1022
Nov 14 '18 at 14:36





have a look at shift or rolling sum functions from other packages.

– mt1022
Nov 14 '18 at 14:36













@mt1022 thank you, maybe this is what I need!

– Francesco Tamberi
Nov 14 '18 at 14:38





@mt1022 thank you, maybe this is what I need!

– Francesco Tamberi
Nov 14 '18 at 14:38













dt[, S := Reduce(`+`, shift(NR, 0:2, type = "lead"))]

– Martin Schmelzer
Nov 14 '18 at 14:39







dt[, S := Reduce(`+`, shift(NR, 0:2, type = "lead"))]

– Martin Schmelzer
Nov 14 '18 at 14:39















@Jimbou you are right, I'll delete asap the question!

– Francesco Tamberi
Nov 14 '18 at 14:44





@Jimbou you are right, I'll delete asap the question!

– Francesco Tamberi
Nov 14 '18 at 14:44












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python