An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090301/cd7f7605/attachment-0002.pl>
Partial sum of a vector
8 messages · Jorge Ivan Velez, jim holtman, Mohammad Sabr +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090301/1a9cb6b7/attachment-0002.pl>
?filter
x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
filter(x, rep(1,4))
Time Series: Start = 1 End = 20 Frequency = 1 [1] NA 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 NA NA
On Sun, Mar 1, 2009 at 9:29 PM, Mohammad Sabr <mohammad_sabr at yahoo.com> wrote:
I am trying to run a loop where I can sum parts of a matrix. For example, I want at each step in the loop to sum the?the next?4-values of a vector. I tried to do the following but the result were wrong:
for (i in 4:T) {
data_q[i-3,]=sum(data_m[i-3,]:data_m[i,])
}
can anyone please direct me on what should I do to run this loop properly
Thanks in advance
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090301/26da0703/attachment-0002.pl>
perhaps this?
M <- dim(data_m)[2]
for(j in 1:M){
for (i in 4:T) {
data_q[i-3,j]=sum(data_m[(i-3):i,j])
}
}
of course, you can vectorize this and speed it up significantly, but
there is something evil about premature optimization.
On Mar 2, 1:29?pm, Mohammad Sabr <mohammad_s... at yahoo.com> wrote:
I am trying to run a loop where I can sum parts of a matrix. For example, I want at each step in the loop to sum the?the next?4-values of a vector. I tried to do the following but the result were wrong:
?
for (i in 4:T) {
data_q[i-3,]=sum(data_m[i-3,]:data_m[i,])}
?
can anyone please direct me on what should I do to run this loop properly
?
Thanks in advance
? ? ? ? [[alternative HTML version deleted]]
______________________________________________ R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090301/758fb0a7/attachment-0002.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090301/1fed7bd4/attachment-0002.pl>
Per the help page rollapply needs to be given a zoo or ts object, so
you just coerce the matrix to zoo:
> data_m <- matrix(1:20, ncol=2)
> data_q <-matrix(,nrow=7,ncol=2)
# then you can vectorize the process
> data_q[,] <-rollapply(as.zoo(data_m[,]),4,FUN=sum)
> data_q
[,1] [,2]
[1,] 10 50
[2,] 14 54
[3,] 18 58
[4,] 22 62
[5,] 26 66
[6,] 30 70
[7,] 34 74
David Winsemius
On Mar 1, 2009, at 10:16 PM, Mohammad Sabr wrote:
> Thanks Jorge,
>
> I tried to use the rollapply function as follows:
>
> rollapply(data_m ,4 , sum, by =1 , by.column = TRUE)
>
> but it is giving me the following error message:
>
> Error in UseMethod("rollapply") : no applicable method for "rollapply"
>
> Is there any file that I need to install or update. I reinstalled
> the zoo package but I still getting the previous problem.
>
> Thank you for your assistance,
>
>
> --- On Sun, 3/1/09, Jorge Ivan Velez <jorgeivanvelez at gmail.com> wrote:
>
> From: Jorge Ivan Velez <jorgeivanvelez at gmail.com>
> Subject: Re: [R] Partial sum of a vector
> To: "Mohammad Sabr" <mohammad_sabr at yahoo.com>
> Cc: r-help at r-project.org
> Date: Sunday, March 1, 2009, 10:49 PM
>
>
> Dear Mohammad,
>
>
> Take a look at the rollmean function in the zoo package.
>
>
> HTH,
>
> Jorge
>
>
> On Sun, Mar 1, 2009 at 9:29 PM, Mohammad Sabr
> <mohammad_sabr at yahoo.com> wrote:
>
> I am trying to run a loop where I can sum parts of a matrix. For
> example, I want at each step in the loop to sum the the next 4-
> values of a vector. I tried to do the following but the result were
> wrong:
>
> for (i in 4:T) {
> data_q[i-3,]=sum(data_m[i-3,]:data_m[i,])
> }
>
> can anyone please direct me on what should I do to run this loop
> properly
>
> Thanks in advance
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.