[R] Iterative Averages
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Aug 2 19:04:53 CEST 2008
Use kronecker products. For example,
> mat <- matrix(1001:1144, 12, 12)
> result <- k1 <- matrix(1:9, 3, 3)
> k2 <- matrix(1, 4, 4) # size of one block
> result[] <- tapply(mat, kronecker(k1, k2), mean)
> result
[,1] [,2] [,3]
[1,] 1020.5 1068.5 1116.5
[2,] 1024.5 1072.5 1120.5
[3,] 1028.5 1076.5 1124.5
> mean(mat[1:4, 1:4]) # check first block
[1] 1020.5
On Sat, Aug 2, 2008 at 9:28 AM, Steve Murray <smurray444 at hotmail.com> wrote:
>
> Dear all,
>
> I have a data frame of 2160 rows and 4320 columns, which I hope to condense to a smaller dataset by finding averages of 6 by 6 blocks of values (to produce a data frame of 360 rows by 720 columns).
>
> How would I go about finding the mean of a 6 x 6 block, then find the mean of the next adjacent 6 x 6 block, and so on, until the whole data frame has been covered?
>
> One slight twist is that I have NA values, which I don't want to be included in the calculations unless a particular 6 x 6 block is entirely composed of NA values - in which case, NA should be the output value.
>
> Thanks very much for any advice and solutions.
>
> Steve
>
> _________________________________________________________________
> Get Hotmail on your mobile from Vodafone
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list