

This post has NOT been accepted by the mailing list yet.
Dear all
I'm new to R, so thanks for your indulgence:)  I have a matrix (154 columns x 1000 rows) a,b,c,d,e,f,... and want to calculate a new matrix (77 columns x 1000 rows) based on the averages of columns ab,cd,ef, etc...
So my question is which is the easiest way in R to get from this:
[1] a, b, c, d, e, f, ....
[2] 1, 2, 5, 9, 1, 4, ...
[3] 2, 3, 9, 6, 5, 7, ...
to this :
[1] ab, cd, ef, ....
[2] 1.5, 7.0, 2.5, ...
[3] 2.5, 7.5, 6.0, ...
Thanks for any help!
Chega Gaglieri


This post was updated on .
# need zoo to use rollapply()
library(zoo)
# your data (I called df)
df < structure(list(a = 1:2, b = 2:3, c = c(5L, 9L), d = c(9L, 6L),
e = c(1L, 5L), f = c(4, 7)), .Names = c("a", "b", "c", "d",
"e", "f"), class = "data.frame", row.names = c(NA, 2L))
# transpose and make a zoo object
df2 < zoo(t(df))
#rollapply to get means and transpose back
means < t(rollapply(df2, width=2, by=2, FUN=mean))
# adding the combined column names you requested
colnames(means) < apply(matrix(names(df), nrow=2), 2, paste, collapse=" ")
means
HTH
Chega wrote
Dear all
I'm new to R, so thanks for your indulgence:)  I have a matrix (154 columns x 1000 rows) a,b,c,d,e,f,... and want to calculate a new matrix (77 columns x 1000 rows) based on the averages of columns ab,cd,ef, etc...
So my question is which is the easiest way in R to get from this:
[1] a, b, c, d, e, f, ....
[2] 1, 2, 5, 9, 1, 4, ...
[3] 2, 3, 9, 6, 5, 7, ...
to this :
[1] ab, cd, ef, ....
[2] 1.5, 7.0, 2.5, ...
[3] 2.5, 7.5, 6.0, ...
Thanks for any help!
Chega Gaglieri


On Thu, Dec 1, 2011 at 7:13 PM, B77S < [hidden email]> wrote:
> # need zoo to use rollapply()
>
> # your data (I called df)
> df < structure(list(a = 1:2, b = 2:3, c = c(5L, 9L), d = c(9L, 6L),
> e = c(1L, 5L), f = c(4, 7)), .Names = c("a", "b", "c", "d",
> "e", "f"), class = "data.frame", row.names = c(NA, 2L))
>
> # transpose and make a zoo object
> df2 < zoo(t(df))
>
> #rollapply to get means and transpose back
> means < t(rollapply(df2, width=2, by=2, FUN=mean))
>
> # adding the combined column names you requested
> colnames(means) < apply(matrix(names(df), nrow=2), 2, paste, collapse=", ")
>
Note that zoo's rollapply also works on plain matrices and vectors.

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1877GKXGROUP
email: ggrothendieck at gmail.com
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Sorry for that, and thanks Gabor,
I could have sworn that it wouldn't.
Gabor Grothendieck wrote
On Thu, Dec 1, 2011 at 7:13 PM, B77S < [hidden email]> wrote:
> # need zoo to use rollapply()
>
> # your data (I called df)
> df < structure(list(a = 1:2, b = 2:3, c = c(5L, 9L), d = c(9L, 6L),
> e = c(1L, 5L), f = c(4, 7)), .Names = c("a", "b", "c", "d",
> "e", "f"), class = "data.frame", row.names = c(NA, 2L))
>
> # transpose and make a zoo object
> df2 < zoo(t(df))
>
> #rollapply to get means and transpose back
> means < t(rollapply(df2, width=2, by=2, FUN=mean))
>
> # adding the combined column names you requested
> colnames(means) < apply(matrix(names(df), nrow=2), 2, paste, collapse=", ")
>
Note that zoo's rollapply also works on plain matrices and vectors.

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1877GKXGROUP
email: ggrothendieck at gmail.com
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


This post has NOT been accepted by the mailing list yet.
This solved my problem  Thanks a lot for your help! Please allow me one more question: Works zoo's rollapply on a plain matrix faster than on a zoo object? I am asking since I wanted to apply the provided averaging code to a larger matrix (2500 rows x 200 cols), which is quite time consuming...
Thanks again!
Chega
________________________________
From: B77S [via R] < [hidden email]>
To: Chega < [hidden email]>
Sent: Friday, December 2, 2011 6:16 AM
Subject: Re: Moving column averaging
Sorry for that, and thanks Gabor,
I could have sworn that it wouldn't.
Gabor Grothendieck wrote
>On Thu, Dec 1, 2011 at 7:13 PM, B77S <[hidden email]> wrote:
>> # need zoo to use rollapply()
>>
>> # your data (I called df)
>> df < structure(list(a = 1:2, b = 2:3, c = c(5L, 9L), d = c(9L, 6L),
>> e = c(1L, 5L), f = c(4, 7)), .Names = c("a", "b", "c", "d",
>> "e", "f"), class = "data.frame", row.names = c(NA, 2L))
>>
>> # transpose and make a zoo object
>> df2 < zoo(t(df))
>>
>> #rollapply to get means and transpose back
>> means < t(rollapply(df2, width=2, by=2, FUN=mean))
>>
>> # adding the combined column names you requested
>> colnames(means) < apply(matrix(names(df), nrow=2), 2, paste, collapse=", ")
>>
>
>Note that zoo's rollapply also works on plain matrices and vectors.
>
>
>Statistics & Software Consulting
>GKX Group, GKX Associates Inc.
>tel: 1877GKXGROUP
>email: ggrothendieck at gmail.com
>
>______________________________________________
>[hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp>PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>and provide commented, minimal, selfcontained, reproducible code.
________________________________
If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Movingcolumnaveragingtp4130179p4143909.htmlTo unsubscribe from Moving column averaging, click here.
NAML


I don't know the answer, but would suppose not.
You could test this for yourself using:
system.time()
example:
system.time(rnorm(100000,0,1))
Chega wrote
This solved my problem  Thanks a lot for your help! Please allow me one more question: Works zoo's rollapply on a plain matrix faster than on a zoo object? I am asking since I wanted to apply the provided averaging code to a larger matrix (2500 rows x 200 cols), which is quite time consuming...
Thanks again!
Chega
________________________________
From: B77S [via R] < [hidden email]>
To: Chega < [hidden email]>
Sent: Friday, December 2, 2011 6:16 AM
Subject: Re: Moving column averaging
Sorry for that, and thanks Gabor,
I could have sworn that it wouldn't.
Gabor Grothendieck wrote
>On Thu, Dec 1, 2011 at 7:13 PM, B77S <[hidden email]> wrote:
>> # need zoo to use rollapply()
>>
>> # your data (I called df)
>> df < structure(list(a = 1:2, b = 2:3, c = c(5L, 9L), d = c(9L, 6L),
>> e = c(1L, 5L), f = c(4, 7)), .Names = c("a", "b", "c", "d",
>> "e", "f"), class = "data.frame", row.names = c(NA, 2L))
>>
>> # transpose and make a zoo object
>> df2 < zoo(t(df))
>>
>> #rollapply to get means and transpose back
>> means < t(rollapply(df2, width=2, by=2, FUN=mean))
>>
>> # adding the combined column names you requested
>> colnames(means) < apply(matrix(names(df), nrow=2), 2, paste, collapse=", ")
>>
>
>Note that zoo's rollapply also works on plain matrices and vectors.
>
>
>Statistics & Software Consulting
>GKX Group, GKX Associates Inc.
>tel: 1877GKXGROUP
>email: ggrothendieck at gmail.com
>
>______________________________________________
>[hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp>PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>and provide commented, minimal, selfcontained, reproducible code.
________________________________
If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Movingcolumnaveragingtp4130179p4143909.htmlTo unsubscribe from Moving column averaging, click here.
NAML

