sum of a subset of rows in a data table [duplicate]
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?
r data.table
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.
add a comment |
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?
r data.table
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 atshift
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
add a comment |
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?
r data.table
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
r data.table
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 atshift
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
add a comment |
1
have a look atshift
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
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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