I have a table with a list of people, each with a different ID. The IDs are replicated 12*n times and each row contains a value. For instance:
I want to calculate the sum of these values every 12 months,i.e the sum of values from row 2:13, 14:25, etc... and that for each person in my data table.
I have done this but it does not seem to work.
for (i in unique(new_table[,"ID"])){
n<-data[n_pers,9]*12 # n differs for each person and is obtained from another table
for (t in 0:n) {
new_table$yearly<-sum(new_table[which(new_table$ID==i),][t*12+1:min(n,(t+1)*12+1) ,"Values"])
}}
No errors are generated. However, I only have NA in my yearly column. Why is that so? Any help would be very much appreciated.