[R] windowing

Bond, Stephen Stephen.Bond at cibc.com
Wed Sep 11 17:36:09 CEST 2013


Very interesting. Does not produce a solution outright, but may be still usable

> df <- data.frame(x1=c(rep(1,10),rep(2,7)),x2=rep(1:17))
> aggregate(df$x2,by=list(x1=df$x1),cumsum)-> a1
> a1
  x1                                   x
1  1 1, 3, 6, 10, 15, 21, 28, 36, 45, 55
2  2          11, 23, 36, 50, 65, 81, 98
> dim(a1)
[1] 2 2
> a1[1,2]
$`0`
 [1]  1  3  6 10 15 21 28 36 45 55

> a1[2,2]
$`1`
[1] 11 23 36 50 65 81 98

> class(a1[2,2])
[1] "list"

If anybody can suggest how to create a reshape-able dataframe out of this, please speak. 
Ideally I should be able to use 

reshape(a1,dir="long",varying=2:11,idvar="x1",v.names="x")

to get it back in long form.
Thank everybody.

Stephen B
-----Original Message-----
From: MacQueen, Don [mailto:macqueen1 at llnl.gov] 
Sent: Wednesday, September 11, 2013 10:42 AM
To: Bond, Stephen; r-help at r-project.org
Subject: Re: [R] windowing

>From the help page for the aggregate function:


Compute Summary Statistics of Data Subsets

Description:

     Splits the data into subsets, computes summary statistics for
     each, and returns the result in a convenient form.




You might have to use cumsum() after the aggregation, if
"unbounded preceding" causes a cumulative sum to be calculated.

-Don
-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 9/9/13 11:58 AM, "Bond, Stephen" <Stephen.Bond at cibc.com> wrote:

>Is there a package or a command that does window aggregation like
>
>select
>sum(col1) over
>(partition by col2, col3 order by col4
>rows between unbounded preceding and current row) as sum1
>from table1 ;
>
>the above is Netezza syntax, but Postgre has same capability.
>
>Stephen B
>
>	[[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.



More information about the R-help mailing list