0

I know how to add a column that is, say, the sum of two other columns, but I'm looking for a way to make a new column that equals the sum of a subset of rows in another column.

For example, I have a table, "table.1" and the third column "table.1[3]" consists of numbers. I want to add a fourth column such that the 1st row of column 4 = the sum of the values in column 3 from row 1 to 100; the 2nd row = sum of column 3 from row 2 to 101, and so on.

Essentially, at row x, I want table.1[x, 4]=sum(table.1[x:x+99, 3])

Anyone know how I can add a column like that? Thanks.

user3593717
  • 131
  • 6
  • Show us the tables (and by tables,I assume you mean data.frames)! Preferably in the form of [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas May 01 '14 at 18:38
  • Say the 'table' has 200 rows. What should happen at say row 102? Now x:x+99 is 102:201 - but there is no row 201. – John Paul May 01 '14 at 18:45
  • well, you could either stop or go on like `x:ifelse(x+99 > nrow(table.1), nrow(table.1), x+99)` – Christian Borck May 01 '14 at 19:01

1 Answers1

0

One way would be to use the embed() command. In this example your "column 3" is named "a" in the data.frame and i'm adding a column named "b" made up such that dd$b[x]<-sum(dd$a[x:(x+4-1)]) so i'm just using a distance of N=4 rather than N=100 for simplicity.

dd<-data.frame(a=c(1,3,6,4,2,4,6,7,8,1))
N<-4
dd$b<-rowSums(embed(c(dd$a, rep.int(0,N-1)), N))

Note that I padded the end of the dd$a vector so that when the range "goes off the end", I assume those values to be 0.

MrFlick
  • 195,160
  • 17
  • 277
  • 295