[R] repeating commands with varying arguments
David Winsemius
dwinsemius at comcast.net
Thu Feb 28 03:11:21 CET 2013
This is no longer a question for which the Subject is accurate. Please do not continue using inaccurate subject lines.
On Feb 27, 2013, at 9:38 AM, Zjoanna 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.
>>
Try:
> lapply(LETTERS[1:4] , function (let) dat1[ which.max( dat1[[let]]), ])
[[1]]
m1 n1 m n A B C D
3 2 3 4 5 0.5 0.6 0.2 0.2
[[2]]
m1 n1 m n A B C D
4 2 4 3 4 0.2 0.8 0.5 0.3
[[3]]
m1 n1 m n A B C D
4 2 4 3 4 0.2 0.8 0.5 0.3
[[4]]
m1 n1 m n A B C D
2 2 2 4 4 0.2 0.1 0.3 0.4
--
David.
>>
>>
>>
>>
>> ________________________________
>> 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.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list