Ash
Ash

Reputation: 73

Cumulative sum for n rows

I have been trying to produce a command in R that allows me to produce a new vector where each row is the sum of 25 rows from a previous vector.

I've tried making a function to do this, this allows me to produce a result for one data point.

I shall put where I haver got to; I realise this is probably a fairly basic question but it is one I have been struggling with... any help would be greatly appreciated;

example<-c(1;200)


fun.1<-function(x)
{sum(x[1:25])}

checklist<-sapply(check,FUN=fun.1)

This then supplies me with a vector of length 200 where all values are NA.

Can anybody help at all?

Upvotes: 0

Views: 3865

Answers (1)

rlegendi
rlegendi

Reputation: 10606

Your example is a bit noisy (e.g., c(1;200) has no meaning, probably you want 1:200 there, or, if you would like to have a list of lists then something like rep, there is no check variable, it should have been example, etc.).

Here's the code what I think you need probably (as far as I was able to understand it):

x <- rep(list(1:200), 5)
f <- function(y) {y[1:20]}
sapply(x, f)

Next time please be more specific, try out the code you post as an example before submitting a question.

Upvotes: 2

Related Questions