Skip to content
Prev 155292 / 398506 Next

Averaging 'blocks' of data

Here is a way to do it by reading in 60 lines at a time and computing the means:

# create some test data
n <- 360
x <- matrix(runif(360*16800), nrow=16800)
cat(x, file="/tempxx.txt")


# now process the data 60 lines at a time, averaging each 60x60 block
result <- matrix(0, nrow=6, ncol=280)
nextLine <- 1  # next output in the result
# create a list of indices to use to partition the input matrix
colIndex <- split(seq(16800), (seq(16800) - 1) %/% 60)
input <- file("/tempxx.txt", "r")
while (TRUE){
    # use 'scan' to read in 60 lines at a time
    block <- scan(input, what=0, n=60*16800)
    if (length(block) != 60 * 16800) break  # exit if done
    # convert to a matrix
    block <- matrix(block, nrow=60, byrow=TRUE)
    # compute the mean and store it
    result[nextLine,] <- sapply(colIndex, function(.blk){
        mean(block[, .blk])
    })
    nextLine <- nextLine + 1
}
On Sun, Sep 7, 2008 at 4:46 PM, Steve Murray <smurray444 at hotmail.com> wrote: