Re: [R] cumsum function with data frame

From: Felipe Carrillo <mazatlanmexico_at_yahoo.com>
Date: Thu, 03 Jun 2010 11:28:58 -0700 (PDT)

You can also use ddply from the plyr package:

library(plyr)
Data <- read.table(textConnection("variable        Year      value

EC01            2005        5
EC01            2006        10
AAO1            2005          2
AAO1          2006          4"),header=T)
Data
ddply(Data,.(variable),summarise,Year=Year,value=value,CUMSUM=cumsum(value))  
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

Data <-
> read.table(textConnection("variable        Year   
>   value

EC01            2005    

>     5
EC01            2006 
>       10
AAO1           
> 2005          2
AAO1       

>   2006          4"),header=T)

Datalist
> <-split(Data,Data$variable)
resultlist <-
> lapply(Datalist,function(x){
    x$cumul <-
> cumsum(x$value)
    return(x)
})
result <-
> unsplit(resultlist,Data$variable)
result

  variable Year value
> cumul

1    EC01 2005    5    5

>   EC01 2006    10    15

3    AAO1
> 2005    2    2

4    AAO1 2006   
> 4    6

On a side note: I've used this construction now for a
> number of problems.

Some could be better solved using more specific functions
> (e.g. ave() for
adding a column with means for example). I'm not sure however
> this is the

most optimal approach to applying a function to subsets of a
> dataframe and

adding the result of that function as an extra variable.
> Anybody care to

elaborate on how the R masters had it in
> mind?

Cheers
Joris

On Thu, Jun 3, 2010 at 5:58 PM, > ymailto="mailto:n.vialma_at_libero.it"
> href="mailto:n.vialma_at_libero.it">n.vialma_at_libero.it <> ymailto="mailto:n.vialma_at_libero.it"
> href="mailto:n.vialma_at_libero.it">n.vialma_at_libero.it>wrote:

>

>
> Dear list,
> I have a problem with the cumsum function.
> I have a
> data frame like the following one
> variable       
> Year      value
> EC01         
>   2005        5
>

> EC01   
>         2006        10
>

>
> AAO1            2005       
>   2
>

> AAO1          2006 
>         4
> what I would like to obtain is
>
> variable        Year      value 
> cumsum
>
>

> EC01           
> 2005        5         
> 5
>
>

> EC01           
> 2006        10       
> 15
>
>

> AAO1           
> 2005          2         
>   2
>
>

> AAO1         
> 2006          4         
>   6
>
>

> if I use the by function or the aggregate
> function the result is a list or
> something else, what I want is a data
> frame as I showed above...
> anyone knows how to get it???
> THANKS
> A LOT
>
>
>
>
>
>        

> [[alternative HTML version deleted]]
>

>
> ______________________________________________
> > ymailto="mailto:R-help_at_r-project.org"
> href="mailto:R-help_at_r-project.org">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.
>
-- 
Joris Meys
Statistical 

> Consultant
Ghent University Faculty of Bioscience
> Engineering
Department of Applied mathematics, biometrics and process
> control
Coupure Links 653 B-9000 Gent tel : +32 9 264 59
> 87
> href="mailto:Joris.Meys_at_Ugent.be">Joris.Meys_at_Ugent.be

------------------------------- Disclaimer
> : http://helpdesk.ugent.be/e-maildisclaimer.php
   
> [[alternative HTML version
> deleted]]
______________________________________________
> ymailto="mailto:R-help_at_r-project.org"
> href="mailto:R-help_at_r-project.org">R-help_at_r-project.org mailing list
> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank
> >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.
Received on Thu 03 Jun 2010 - 18:34:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Jun 2010 - 19:40:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive