Question

I have what should be a simple question, but I'm very new to R so it's stumping me.

I have a series of 25 columns representing trials in a behavioral experiment. I would like to use roll apply to sum the first 5 columns, then 6-10, 11-15, and so on, so that I end up with a new, 5 column data frame containing the output (similar to the 5 column sample below). Really, the point is to be able to rapidly change the "bin size" so that I can make decisions about what "resolution" best suits the data. In the end I wont just be summing, but I think this answer would be plenty to get me rolling.

INPUT:
Col1   Col2   Col3   Col4   Col5
  1      1      1      1      1
  2      2      2      2      2

DESIRED OUTPUT:
Col1
 5
 10
Was it helpful?

Solution

Assuming INPUT is a dataframe, maybe something like:

sapply(0:((length(LENGTH)-1)/5), function(x) sum(INPUT[, 5*x+1:5]) )

I don't think zoo::rollapply was conceived of as rolling across columns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top