[R] windowing

arun smartpink111 at yahoo.com
Wed Sep 11 17:50:17 CEST 2013


Hi,
Try:
library(plyr)
 ddply(df,.(x1),summarize,x=cumsum(x2))
#   x1  x
#1   1  1
#2   1  3
#3   1  6
#4   1 10
#5   1 15
#6   1 21
#7   1 28
#8   1 36
#9   1 45
#10  1 55
#11  2 11
#12  2 23
#13  2 36
#14  2 50
#15  2 65
#16  2 81
#17  2 98


#or using a1

 df2<- data.frame(x1=rep(a1$x1,sapply(a1$x,length)),x=unlist(a1$x))
row.names(df2)<-1:nrow(df2)


A.K.



----- Original Message -----
From: "Bond, Stephen" <Stephen.Bond at cibc.com>
To: "'MacQueen, Don'" <macqueen1 at llnl.gov>; "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, September 11, 2013 11:36 AM
Subject: Re: [R] windowing

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.

______________________________________________
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