[R] cumulative sum by group and under some criteria

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Feb 28 01:06:49 CET 2013


Read The Fine Manual... the document "An Introduction to R" is supplied with R, and section 10 discusses functions. (Macros are not supported.) Depending on what you want to parameterize, a review of indexing and lists (section 2.7, 5.3, and 6) is likely to be needed as well. This is a valuable document to be familiar with, though I recommend avoiding the use of attach/detach discussed in 6.3.2.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Zjoanna <Zjoanna2013 at gmail.com> wrote:

>Hi,
>I need to change one or several parameters and run the same code
>several
>times, how to create a macro with parameters in R?
>
>On Wed, Feb 27, 2013 at 7:45 AM, arun kirshna [via R] <
>ml-node+s789695n4659801h42 at n4.nabble.com> wrote:
>
>> dat1<- read.table(text="
>> m1 n1 m n    A      B    C   D
>> 2    2    4  5  0.1    0.2  0.2 0.3
>> 2   2     4  4  0.2   0.1   0.3  0.4
>> 2   3     4  5  0.5   0.6   0.2  0.2
>> 2  4      3  4  0.2   0.8   0.5  0.3
>> ",sep="",header=TRUE)
>>
>>
>> dat1[which.max(dat1$D),]
>> #  m1 n1 m n   A   B   C   D
>> #2  2  2 4 4 0.2 0.1 0.3 0.4
>> A.K.
>>
>>
>>
>>
>>
>> ________________________________
>> From: Joanna Zhang <[hidden
>email]<http://user/SendEmail.jtp?type=node&node=4659801&i=0>>
>>
>> To: arun <[hidden
>email]<http://user/SendEmail.jtp?type=node&node=4659801&i=1>>
>>
>> Sent: Wednesday, February 27, 2013 1:25 AM
>> Subject: Re: [R] cumulative sum by group and under some criteria
>>
>>
>> Thanks very much! it works!
>>
>> suppose that I have a data:
>> m1 n1 m n    A      B    C   D
>> 2    2    4  5  0.1    0.2  0.2 0.3
>> 2   2     4  4  0.2   0.1   0.3  0.4
>> I want to identify the max value of D and extract the row , in this
>> example, it is the 2nd row.
>>
>>
>> On Tue, Feb 26, 2013 at 11:13 PM, arun <[hidden
>email]<http://user/SendEmail.jtp?type=node&node=4659801&i=2>>
>> wrote:
>>
>> res2<- join(res1,d3,by=c("m1","n1"),type="inner")
>>
>> >
>> >p0L<-0.05
>> >p0H<-0.05
>> >p1L<-0.20
>> >p1H<-0.20
>> >
>> >res2<- within(res2,{p1<- x/m; p2<- y/n;term2_p0<-dbinom(x1,m1, p0L,
>> log=FALSE)* dbinom(y1,n1,p0H, log=FALSE)*dbinom(x-x1,m-m1, p0L,
>log=FALSE)*
>> dbinom(y-y1,n-n1,p0H, log=FALSE);term2_p1<- dbinom(x1,m1, p1L,
>log=FALSE)*
>> dbinom(y1,n1,p1H, log=FALSE)*dbinom(x-x1,m-m1, p1L, log=FALSE)*
>> dbinom(y-y1,n-n1,p1H, log=FALSE)})
>> >
>> >res4<-do.call(rbind,lapply(seq_len(nrow(res2)),function(i)
>> {Pm2<-rbeta(1000,0.2+res2[i,"x"],0.8+res2[i,"m"]-res2[i,"x"]);Pn2<-
>> rbeta(1000,0.2+res2[i,"y"],0.8+res2[i,"n"]-res2[i,"y"]); Fm2<-
>ecdf(Pm2);
>> Fn2<- ecdf(Pn2); Fmm2<- Fm2(res2[i,"p1"]); Fnn2<-
>Fn2(res2[i,"p2"]);R2<-
>> (Fmm2+Fnn2)/2; Fmm_f2<- min(R2, Fmm2); Fnn_f2<- max(R2, Fnn2); Qm2<-
>> 1-Fmm_f2; Qn2<-
>1-Fnn_f2;data.frame(Fmm2,Fnn2,R2,Fmm_f2,Fnn_f2,Qm2,Qn2)}))
>> >
>> >res5<-cbind(res2,res4)
>> > head(res5,5)
>> >#  m1 n1 x1 y1 m n x y cterm1_P0L cterm1_P1L cterm1_P0H cterm1_P1H
>> term2_p1
>> >#1  2  2  0  0 4 4 0 0     0.9025       0.64     0.9025       0.64
>> 0.16777216
>> >#2  2  2  0  0 4 4 0 1     0.9025       0.64     0.9025       0.64
>> 0.08388608
>> >#3  2  2  0  0 4 4 0 2     0.9025       0.64     0.9025       0.64
>> 0.01048576
>> >#4  2  2  0  0 4 4 1 0     0.9025       0.64     0.9025       0.64
>> 0.08388608
>> >#5  2  2  0  0 4 4 1 1     0.9025       0.64     0.9025       0.64
>> 0.04194304
>> >#     term2_p0   p2   p1 Fmm2  Fnn2     R2 Fmm_f2 Fnn_f2   Qm2   Qn2
>> >#1 0.663420431 0.00 0.00 0.00 0.000 0.0000  0.000  0.000 1.000 1.000
>> >#2 0.069833730 0.25 0.00 0.00 0.601 0.3005  0.000  0.601 1.000 0.399
>> >#3 0.001837730 0.50 0.00 0.00 0.612 0.3060  0.000  0.612 1.000 0.388
>> >#4 0.069833730 0.00 0.25 0.59 0.000 0.2950  0.295  0.295 0.705 0.705
>> >#5 0.007350919 0.25 0.25 0.60 0.566 0.5830  0.583  0.583 0.417 0.417
>> >
>> >
>> >A.K.
>> >
>> >
>> >
>> >
>> >>[hidden email]
><http://user/SendEmail.jtp?type=node&node=4659801&i=3>mailing list
>> >>
>> >>https://stat.ethz.ch/mailman/listinfo/r-help
>> >>PLEASE do read the posting guide
>>
>http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> >>and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>> ______________________________________________
>> [hidden email]
><http://user/SendEmail.jtp?type=node&node=4659801&i=4>mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>>
>http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the
>discussion
>> below:
>>
>>
>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4659801.html
>>  To unsubscribe from cumulative sum by group and under some criteria,
>click
>>
>here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4657074&code=WmpvYW5uYTIwMTNAZ21haWwuY29tfDQ2NTcwNzR8LTE3NTE1MDA0MzY=>
>> .
>>
>NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/cumulative-sum-by-group-and-under-some-criteria-tp4657074p4659813.html
>Sent from the R help mailing list archive at Nabble.com.
>	[[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