loops - Summing specific data from column in r -


im trying creat function in r let me sum data last 3 rows new column (i.e. if im looking @ day 4 = days 3+2+1)

this i've worked out far, doesnt work.

s3<- function(x){ res <- numeric(nrow(x)) (i in 1:nrow(x)){     res[i] <- if (i > 3) {                res[i] <- x[i-3,10] } else {   res[i] <- x[i,10] } } x$pp3 <- res return(x) } 

use this:

x$pp3 <- x[,10] + c(0,head(x[,10],-1)) + c(0,0,head(x[,10],-2)) 

if want function:

s3 <- function(x, j=10){     within(x, pp3 <- x[,j] + c(0,head(x[,j],-1)) + c(0,0,head(x[,j],-2))) } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -