-2

I would like to be able to sum the value in each row with the value below and assign the result to a new column in R.

The following code (nearly) achieves what I want using a for loop (except that I have to manually do the last row - which is not really a problem). I would like to do a similar thing but using an apply function since the for loop is very slow on my large dataset - however I can't figure out the apply syntax.

data<-data.frame(runif(10))

data$x<-
 for (i in 1:nrow(data)) {
   data[i,2 ] <- data[i,1]+data[i+1,1]
                          }   
Cœur
  • 37,241
  • 25
  • 195
  • 267
SR_111
  • 89
  • 4
  • What does the last row sum with? Zero? – Tav Jul 12 '16 at 09:21
  • 1
    Make some research before asking questions. Question already exist [here](http://stackoverflow.com/questions/19200841/consecutive-rolling-sums-in-a-vector-in-r?noredirect=1&lq=1). And read all the answer when pepole take time to answer to you before validate one. – Christophe D. Jul 12 '16 at 14:01

3 Answers3

1

Use rollSum (package RcppRoll)

You can use the function roll_sum(x, nb) which allow you to do a (consecutive) rolling sum of a vector.

if we take your example:

data<-data.frame(x=runif(10000000))  
data$sum<-roll_sum(data$x, 1)

There is also the rollapply(x, nb, fct) from the package zoobut is less performant.

you can see a micro benchmark here

Transform your data

Maybe just transforming you vector by creating a copy a him less the first value and with 0 on the last value will work.

data<-data.frame(x=runif(10))
data$copy<-c(data$x[-1],0)
data$sum<-rowSums(data)
Community
  • 1
  • 1
Christophe D.
  • 1,089
  • 11
  • 21
0

We can get the sum of the current value with the previous value by removing one value from either end of the column and conctenate with NA.

data$x <- c(data[,1][-1] + data[,1][-nrow(data)], NA)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can make a vector that is off by 1 (it starts from the second component) and add an NA at the end of it. Then do the sum.

data<-data.frame(runif(10))
col1 <- data$runif.10.
col1.off <- col1[2:length(col1)]
col1.off <- c(col1.off, NA)
data$x <- col1 + col1.off
Sergio.pv
  • 1,380
  • 4
  • 14
  • 23