Re: [R] Function help
Hi Alexander, I suspect that when you write "try" you mean that you try to run the function with some value for "pid". The "unexpected symbol" error message usually includes the offending symbol and that will probably identify the problem. Jim On Mon, Oct 26, 2015 at 1:20 PM, wrote: > Hello, > > I'm following an example in the book, analyzing baseball data with R, but > it's not working for me. The example is: > > compute.hr <- function(pid){d <- subset(Batting.60, playerID==pid) > sum(d$HR)} > > Every time I try this, it says there's an unexpected symbol. Any idea on > what the unexpected symbol is or how to fix it? Thanks. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Function help
On 25/10/2015 10:20 PM, alexander.thoma...@louisville.edu wrote: > Hello, > > I'm following an example in the book, analyzing baseball data with R, but > it's not working for me. The example is: We don't know what "the book" is. If this is the textbook for your class, you should ask your instructor for help. > > compute.hr <- function(pid){d <- subset(Batting.60, playerID==pid) > sum(d$HR)} That works for me. Duncan Murdoch > > Every time I try this, it says there's an unexpected symbol. Any idea on what > the unexpected symbol is or how to fix it? Thanks. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Function help
Hello, I'm following an example in the book, analyzing baseball data with R, but it's not working for me. The example is: compute.hr <- function(pid){d <- subset(Batting.60, playerID==pid) sum(d$HR)} Every time I try this, it says there's an unexpected symbol. Any idea on what the unexpected symbol is or how to fix it? Thanks. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] R function help!
Pete: Please don't do people's homework for them! At most, give them a ***hint*** only. cheers, Rolf Turner On 02/19/2013 12:01 PM, Pete Brecknock wrote: simonj16 wrote Consider an urn that contains 10 tickets, labelled: 1,1,1,1,2,5,5,10,10,10 I want to draw with replacement n=40 tickets. I am interested in the sum, Y, of the 40 ticket values that I draw Write an R function named urn.model that simulates this experiement. What I have below is not working. flip.n = function(p,n) { return(runif(n,0,1) < p) } ticket.ns<-c(1,1,1,1,2,5,5,10,10,10) urn.model = function(ticket.ns) { draws.per.sim = 1 prob = .1 urn.results = rep(-1, ticket.ns) for (i in 1:ticket.ns) { draws = flip.n(prob,draws.per.sim) num =sum(draws,ticket.ns) urn.results[i] = num } return(urn.results) } urn.25.samples =urn.model(25) urn.25.samples Follow up question: Use urn.model to generate a sample y={y1,...,y25) of n=25 observed sums. Any good? ticket.ns<-c(1,1,1,1,2,5,5,10,10,10) draw=NULL for (i in 1:25){ draw[i] <- sum(sample(ticket.ns,40,replace=TRUE)) } print(draw) HTH Pete __ R-help@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.
Re: [R] R function help!
simonj16 wrote > Consider an urn that contains 10 tickets, labelled: 1,1,1,1,2,5,5,10,10,10 > > I want to draw with replacement n=40 tickets. I am interested in the sum, > Y, of the 40 ticket values that I draw > > Write an R function named urn.model that simulates this experiement. What > I have below is not working. > > flip.n = function(p,n) { > return(runif(n,0,1) < p) > } > ticket.ns<-c(1,1,1,1,2,5,5,10,10,10) > urn.model = function(ticket.ns) { > draws.per.sim = 1 > prob = .1 > urn.results = rep(-1, ticket.ns) > for (i in 1:ticket.ns) { > draws = flip.n(prob,draws.per.sim) > num =sum(draws,ticket.ns) > urn.results[i] = num > } > return(urn.results) > } > urn.25.samples =urn.model(25) > > urn.25.samples > > Follow up question: > > Use urn.model to generate a sample y={y1,...,y25) of n=25 observed sums. Any good? ticket.ns<-c(1,1,1,1,2,5,5,10,10,10) draw=NULL for (i in 1:25){ draw[i] <- sum(sample(ticket.ns,40,replace=TRUE)) } print(draw) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/R-function-help-tp4658998p4659001.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] function help?
Hi there Petr, Apologies for only replying to your post now - I hope the code included below helps you out. An alternative function documentation approach would be the one I took when faced with a similar problem - I wrote some functions to allow me to embed a retrievable function specification into the head of any function I wrote. The code is included below. How to use this code: Put the function described below (extractFunctionDefinition) in a single file (I keep it in FunctionDescription.R) and load this file at the start of every R project you wish to use it. --START FUNCTION extractFunctionDefinition<-function(function_name,start_flag="#<-BEGIN-FUNCTION-DESCRIPTION-BLOCK->", end_flag="#<-END-FUNCTION-DESCRIPTION-BLOCK->") { #<-BEGIN-FUNCTION-DESCRIPTION-BLOCK-> # Extracts this function description from a function. # # Declared In: # FunctionDescription.R # # Args: # function_name: The name of the function (as a string) # start_flag: Flag (as a string) indicating the start of the function description block # (default is #<-BEGIN-FUNCTION-DESCRIPTION-BLOCK->) # end_flag: Flag (as a string) indicating the end of the function description block #(default is #<-END-FUNCTION-DESCRIPTION-BLOCK->) # # Returns: # METHOD_OK if a description block exists, METHOD_FAILED otherwise #<-END-FUNCTION-DESCRIPTION-BLOCK-> #Get function type - this allows us to handle either function name entered as text #or the function name entered as a function pointer (ie myfunc and not "myfunc") if(is.null(attributes(function_name))) { localText<-lapply(attributes(get(function_name)), function(x) return(gsub("\t","",x)))$source }else { localText<-lapply(attributes(function_name), function(x) return(gsub("\t","",x)))$source } descSP<-which(unlist(lapply(localText, function(x){if( length(grep(start_flag, x))==0 ){return(FALSE)}else{return(TRUE)}} ))) descEP<-which(unlist(lapply(localText, function(x){if( length(grep(end_flag, x))==0 ){return(FALSE)}else{return(TRUE)}} ))) if(length(descSP)==0 | length(descEP)==0) { cat(paste("Error - function ", function_name," has no function description included.\r\n",sep="")) return(invisible(METHOD_FAILED)) }else { if(as.character(match.call())[2]=="extractFunctionDefinition"){ ##Handle exceptional case where function is describing itself: descSP<-descSP[2]+1 descEP<-descEP[3]-1 }else{ if(length(descSP)>1){descSP<-descSP[2]+1}else{descSP<-descSP+1} if(length(descEP)>1){descEP<-descEP[2]-1}else{descEP<-descEP-1} } } #Extract function description: desc<-localText[descSP:descEP] cat(paste(desc, collapse="\r\n"),"\r\n") return(invisible(METHOD_OK)) } --END FUNCTION --START DEMO CODE Example Function: flyingPigs<-function(input1, input2) { #<-BEGIN-FUNCTION-DESCRIPTION-BLOCK-> # Demo function demonstrating how to use extractFunctionDefinition() # # Declared In: # # # Args: # input1: anything really, just to prove a point # input2: just to demo how I document my input # # Returns: # I always return a list list(EXIT_STATUS,) #<-END-FUNCTION-DESCRIPTION-BLOCK-> return(list(0,input1, input2)) } extractFunctionDefinition(flyingPigs) extractFunctionDefinition("flyingPigs") --END DEMO CODE- To the rest of the R community: I realise that this is not "how things should be done" - by the time I'd realised that I should be writing a package I'd already created a whole slew of tools! That'll teach me not to read the manual. Cormac Long. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] function help?
Hi r-help-boun...@r-project.org napsal dne 17.09.2010 17:02:29: > On 9/16/10 5:00 PM, Rolf Turner wrote: > > On 17/09/2010, at 8:51 AM, Duke wrote: > > > >> Hi Duncan, > >> > >> On 9/16/10 3:47 PM, Duncan Murdoch wrote: > >>> On 16/09/2010 3:40 PM, Duke wrote: > Hi all, > > I am writing a function (fun.R), but I dont know how to code the > function so that the Help Text will be shown up when one types ?fun (of > course, after he loads it up). Anyone has any advice for me how to do > that? > > > >>> The help text is separate from the function (unless you use the > >>> roxygen package or similar). You need to write a package so that R > >>> will find the help; instructions are in the Writing R Extensions manual. > >> I was expecting something simpler than writing a package, and that I can > >> integrate it into fun.R, but thanks anyway. > > (a) Writing a package is not all that hard. > > You can say so if you are fluent in R. I just started and have written > only 20-30 lines of codes so far. Also, writing a package is still more > complicated than writing a simple function (single file). Why R is so > different from other languages (python, matlab or more basics like C/C++ > etc...)? In fact I still feel very uncomfortable with R, for example, I > hate when I type a function name, it shows the whole function. > > Honestly I feel a world of programming is a mess, and it is tough when > one wants to jump from one language to another. If you just want your function to start each time you start R put it in a file like myfun1.r and source this file each time you start R. Modify file Rprofile.site in etc directory to do source during start up or put file .Rprofile into your working directory. Keeping several functions in a package is an option, however I must agree that for those who do not do it regularly it is a bit cumbersome. Regards Petr > > > (b) It focuses the mind wondrously. > > I did try to write a simple package (followed a tutorial), but did not > find what you said :). Maybe I need more practice. > > > (c) Using R CMD check on the package is very useful for picking the > > lint off the code. > > > > It would be great if you share your experience a little more about this. > > Thanks, > > D. > > >cheers, > > > > Rolf Turner > > > > ## > > Attention: > > This e-mail message is privileged and confidential. If you are not the > > intended recipient please delete the message and notify the sender. > > Any views or opinions presented are solely those of the author. > > > > This e-mail has been scanned and cleared by MailMarshal > > www.marshalsoftware.com > > ## > > > > __ > R-help@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@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.
Re: [R] function help?
On 9/17/10 12:46 PM, Jeff Newmiller wrote: If having the definition of the function appear when you enter the name of the function elicits a "hate" response, then perhaps you should not be using R. That characteristic of R is fundamental and unlikely to change: in R everything is an object, and the result of evaluating an expression is displayed at the command line. Other languages have different unifying principles, and may be appropriate for different tasks. Well, *hate* or *love* is just a matter of time and / or habit, so it will not interfere with me doing anything, especially in work related. I dont mind learning anything that will serve me well. Indeed, some projects that I am currently working with involve some R packages and hence some R coding as well, so I will use R no matter if I hate it or not. Anything that can be done in R could be done in, say, C, but by the time you did so you might find yourself learning very similar lessons to the ones R assumes you know. I have zero background on programming, so I prefer to work and learn in C/C++ if I have a chance (and time). But if I need something quick, then I will have to use something like matlab, R or python. "Duke" wrote: On 9/16/10 5:00 PM, Rolf Turner wrote: On 17/09/2010, at 8:51 AM, Duke wrote: Hi Duncan, On 9/16/10 3:47 PM, Duncan Murdoch wrote: On 16/09/2010 3:40 PM, Duke wrote: Hi all, I am writing a function (fun.R), but I dont know how to code the function so that the Help Text will be shown up when one types ?fun (of course, after he loads it up). Anyone has any advice for me how to do that? The help text is separate from the function (unless you use the roxygen package or similar). You need to write a package so that R will find the help; instructions are in the Writing R Extensions manual. I was expecting something simpler than writing a package, and that I can integrate it into fun.R, but thanks anyway. (a) Writing a package is not all that hard. You can say so if you are fluent in R. I just started and have written only 20-30 lines of codes so far. Also, writing a package is still more complicated than writing a simple function (single file). Why R is so different from other languages (python, matlab or more basics like C/C++ etc...)? In fact I still feel very uncomfortable with R, for example, I hate when I type a function name, it shows the whole function. Honestly I feel a world of programming is a mess, and it is tough when one wants to jump from one language to another. (b) It focuses the mind wondrously. I did try to write a simple package (followed a tutorial), but did not find what you said :). Maybe I need more practice. (c) Using R CMD check on the package is very useful for picking the lint off the code. It would be great if you share your experience a little more about this. Thanks, D. cheers, Rolf Turner ## Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ## __ R-help@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. --- Jeff NewmillerThe . . Go Live... DCN: Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. __ R-help@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.
Re: [R] function help?
If having the definition of the function appear when you enter the name of the function elicits a "hate" response, then perhaps you should not be using R. That characteristic of R is fundamental and unlikely to change: in R everything is an object, and the result of evaluating an expression is displayed at the command line. Other languages have different unifying principles, and may be appropriate for different tasks. Anything that can be done in R could be done in, say, C, but by the time you did so you might find yourself learning very similar lessons to the ones R assumes you know. "Duke" wrote: > On 9/16/10 5:00 PM, Rolf Turner wrote: >> On 17/09/2010, at 8:51 AM, Duke wrote: >> >>> Hi Duncan, >>> >>> On 9/16/10 3:47 PM, Duncan Murdoch wrote: On 16/09/2010 3:40 PM, Duke wrote: >Hi all, > > I am writing a function (fun.R), but I dont know how to code the > function so that the Help Text will be shown up when one types ?fun (of > course, after he loads it up). Anyone has any advice for me how to do > that? > > The help text is separate from the function (unless you use the roxygen package or similar). You need to write a package so that R will find the help; instructions are in the Writing R Extensions manual. >>> I was expecting something simpler than writing a package, and that I can >>> integrate it into fun.R, but thanks anyway. >> (a) Writing a package is not all that hard. > >You can say so if you are fluent in R. I just started and have written >only 20-30 lines of codes so far. Also, writing a package is still more >complicated than writing a simple function (single file). Why R is so >different from other languages (python, matlab or more basics like C/C++ >etc...)? In fact I still feel very uncomfortable with R, for example, I >hate when I type a function name, it shows the whole function. > >Honestly I feel a world of programming is a mess, and it is tough when >one wants to jump from one language to another. > >> (b) It focuses the mind wondrously. > >I did try to write a simple package (followed a tutorial), but did not >find what you said :). Maybe I need more practice. > >> (c) Using R CMD check on the package is very useful for picking the >> lint off the code. >> > >It would be great if you share your experience a little more about this. > >Thanks, > >D. > >> cheers, >> >> Rolf Turner >> >> ## >> Attention: >> This e-mail message is privileged and confidential. If you are not the >> intended recipient please delete the message and notify the sender. >> Any views or opinions presented are solely those of the author. >> >> This e-mail has been scanned and cleared by MailMarshal >> www.marshalsoftware.com >> ## >> > >__ >R-help@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. --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. __ R-help@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.
Re: [R] function help?
On 9/16/10 5:00 PM, Rolf Turner wrote: On 17/09/2010, at 8:51 AM, Duke wrote: Hi Duncan, On 9/16/10 3:47 PM, Duncan Murdoch wrote: On 16/09/2010 3:40 PM, Duke wrote: Hi all, I am writing a function (fun.R), but I dont know how to code the function so that the Help Text will be shown up when one types ?fun (of course, after he loads it up). Anyone has any advice for me how to do that? The help text is separate from the function (unless you use the roxygen package or similar). You need to write a package so that R will find the help; instructions are in the Writing R Extensions manual. I was expecting something simpler than writing a package, and that I can integrate it into fun.R, but thanks anyway. (a) Writing a package is not all that hard. You can say so if you are fluent in R. I just started and have written only 20-30 lines of codes so far. Also, writing a package is still more complicated than writing a simple function (single file). Why R is so different from other languages (python, matlab or more basics like C/C++ etc...)? In fact I still feel very uncomfortable with R, for example, I hate when I type a function name, it shows the whole function. Honestly I feel a world of programming is a mess, and it is tough when one wants to jump from one language to another. (b) It focuses the mind wondrously. I did try to write a simple package (followed a tutorial), but did not find what you said :). Maybe I need more practice. (c) Using R CMD check on the package is very useful for picking the lint off the code. It would be great if you share your experience a little more about this. Thanks, D. cheers, Rolf Turner ## Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ## __ R-help@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.
Re: [R] function help?
On 16/09/2010 5:00 PM, Rolf Turner wrote: On 17/09/2010, at 8:51 AM, Duke wrote: Hi Duncan, On 9/16/10 3:47 PM, Duncan Murdoch wrote: On 16/09/2010 3:40 PM, Duke wrote: Hi all, I am writing a function (fun.R), but I dont know how to code the function so that the Help Text will be shown up when one types ?fun (of course, after he loads it up). Anyone has any advice for me how to do that? The help text is separate from the function (unless you use the roxygen package or similar). You need to write a package so that R will find the help; instructions are in the Writing R Extensions manual. I was expecting something simpler than writing a package, and that I can integrate it into fun.R, but thanks anyway. (a) Writing a package is not all that hard. (b) It focuses the mind wondrously. It's not at all like being hanged! Duncan Murdoch (c) Using R CMD check on the package is very useful for picking the lint off the code. cheers, Rolf Turner ## Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ## __ R-help@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.
Re: [R] function help?
On 17/09/2010, at 8:51 AM, Duke wrote: > Hi Duncan, > > On 9/16/10 3:47 PM, Duncan Murdoch wrote: >> On 16/09/2010 3:40 PM, Duke wrote: >>> Hi all, >>> >>> I am writing a function (fun.R), but I dont know how to code the >>> function so that the Help Text will be shown up when one types ?fun (of >>> course, after he loads it up). Anyone has any advice for me how to do >>> that? >>> >>> >> >> The help text is separate from the function (unless you use the >> roxygen package or similar). You need to write a package so that R >> will find the help; instructions are in the Writing R Extensions manual. > > I was expecting something simpler than writing a package, and that I can > integrate it into fun.R, but thanks anyway. (a) Writing a package is not all that hard. (b) It focuses the mind wondrously. (c) Using R CMD check on the package is very useful for picking the lint off the code. cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ R-help@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.
Re: [R] function help?
Hi Duncan, On 9/16/10 3:47 PM, Duncan Murdoch wrote: On 16/09/2010 3:40 PM, Duke wrote: Hi all, I am writing a function (fun.R), but I dont know how to code the function so that the Help Text will be shown up when one types ?fun (of course, after he loads it up). Anyone has any advice for me how to do that? The help text is separate from the function (unless you use the roxygen package or similar). You need to write a package so that R will find the help; instructions are in the Writing R Extensions manual. I was expecting something simpler than writing a package, and that I can integrate it into fun.R, but thanks anyway. D. Duncan Murdoch __ R-help@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.
Re: [R] function help?
On 16/09/2010 3:40 PM, Duke wrote: Hi all, I am writing a function (fun.R), but I dont know how to code the function so that the Help Text will be shown up when one types ?fun (of course, after he loads it up). Anyone has any advice for me how to do that? The help text is separate from the function (unless you use the roxygen package or similar). You need to write a package so that R will find the help; instructions are in the Writing R Extensions manual. Duncan Murdoch __ R-help@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] function help?
Hi all, I am writing a function (fun.R), but I dont know how to code the function so that the Help Text will be shown up when one types ?fun (of course, after he loads it up). Anyone has any advice for me how to do that? Thanks, D. __ R-help@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.
Re: [R] Function Help
cls59 wrote: > > > > jimdare wrote: >> >> Hi there, >> >> I have created the function below: >> >> pirate<-function(x){ >> a<-x-1; b<-a/5; c<-a-b; >> d<-c-1; e<-d/5; f<-d-e; >> g<-f-1; h<-g/5; i<-g-h; >> j<-i-1; k<-j/5; l<-j-k; >> m<-l-1; n<-m/5; o<-m-n; >> final<-o/5; >> >> final >> } >> >> I want to run this function until the output ('final') is an exact >> integer (e.g. 893.0 rather than 893.78332). I then need to find out >> what value of X (input) resulted in this integer. Could someone please >> help? I am relatively inexperienced at creating functions. >> >> Kind regards, >> James >> >> > > > You could approach this problem by setting it up as an optimization > problem where the task is to guess the value of x such that the squared > difference between pirate(x) and your target value is minimized. The > difference is squared in order to ensure the optimizer attempts to drive > the difference to 0 instead of -infinity. A function that calculates the > squared difference between any function evaluated at a point x and some > target value is: > > sqDiff <- function( x, funObj, target ){ > > return( ( target - funObj(x) )^2 ) > > } > > You can then use R's nlm() function in an attempt to discover the value of > x that results in your target value of 893. This requires specifying an > initial guess p: > > nlm( sqDiff, p = 5000, funObj = pirate, target = 893 ) > > $minimum > [1] 1.996023e-07 > > $estimate > [1] 13634.3 > > $gradient > [1] -3.72529e-14 > > $code > [1] 1 > > $iterations > [1] 3 > > > The solver came up with an estimate of 13634.3 for x: > > pirate( 13634.3 ) > [1] 892.9996 > > Achieving a solution that provides the exact value of 893 is very unlikely > using numerical optimization since these techniques involve refining a > series of guesses until one of the guesses is "good enough"-- i.e. within > 1*10^{-6} of your target value-- at which point the algorithm terminates. > > To get an answer that provides the target number of 893 exactly you will > probably have to use a symbolic solver-- the Ryacas or rSymPy packages > provide interfaces to computer algebra systems that may have the necessary > symbolic tools to help you here. > > Good luck! > > -Charlie > If you put all of this into Yacas (or Mathematica) you get: In> Simplify(final) Out> (4*(256*x-2101))/15625 You can then pick your solution: In> Solve(z==1,x) Out> {x==24029/1024} Then: pirate(24029/1024) is exactly 1 In> Solve(z==893,x) Out> {x==13961529/1024} pirate(13961529/1024) is exactly 893 and so forth. -- View this message in context: http://www.nabble.com/Function-Help-tp25848627p25849340.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Function Help
jimdare wrote: > > Hi there, > > I have created the function below: > > pirate<-function(x){ > a<-x-1; b<-a/5; c<-a-b; > d<-c-1; e<-d/5; f<-d-e; > g<-f-1; h<-g/5; i<-g-h; > j<-i-1; k<-j/5; l<-j-k; > m<-l-1; n<-m/5; o<-m-n; > final<-o/5; > > final > } > > I want to run this function until the output ('final') is an exact integer > (e.g. 893.0 rather than 893.78332). I then need to find out what > value of X (input) resulted in this integer. Could someone please help? > I am relatively inexperienced at creating functions. > > Kind regards, > James > > You could approach this problem by setting it up as an optimization problem where the task is to guess the value of x such that the squared difference between pirate(x) and your target value is minimized. The difference is squared in order to ensure the optimizer attempts to drive the difference to 0 instead of -infinity. A function that calculates the squared difference between any function evaluated at a point x and some target value is: sqDiff <- function( x, funObj, target ){ return( ( target - funObj(x) )^2 ) } You can then use R's nlm() function in an attempt to discover the value of x that results in your target value of 893. This requires specifying an initial guess p: nlm( sqDiff, p = 5000, funObj = pirate, target = 893 ) $minimum [1] 1.996023e-07 $estimate [1] 13634.3 $gradient [1] -3.72529e-14 $code [1] 1 $iterations [1] 3 The solver came up with an estimate of 13634.3 for x: pirate( 13634.3 ) [1] 892.9996 Achieving a solution that provides the exact value of 893 is very unlikely using numerical optimization since these techniques involve refining a series of guesses until one of the guesses is "good enough"-- i.e. within 1*10^{-6} of your target value-- at which point the algorithm terminates. To get an answer that provides the target number of 893 exactly you will probably have to use a symbolic solver-- the Ryacas or rSymPy packages provide interfaces to computer algebra systems that may have the necessary symbolic tools to help you here. Good luck! -Charlie - Charlie Sharpsteen Undergraduate Environmental Resources Engineering Humboldt State University -- View this message in context: http://www.nabble.com/Function-Help-tp25848627p25849148.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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] Function Help
Hi there, I have created the function below: pirate<-function(x){ a<-x-1; b<-a/5; c<-a-b; d<-c-1; e<-d/5; f<-d-e; g<-f-1; h<-g/5; i<-g-h; j<-i-1; k<-j/5; l<-j-k; m<-l-1; n<-m/5; o<-m-n; final<-o/5; final } I want to run this function until the output ('final') is an exact integer (e.g. 893.0 rather than 893.78332). I then need to find out what value of X (input) resulted in this integer. Could someone please help? I am relatively inexperienced at creating functions. Kind regards, James -- View this message in context: http://www.nabble.com/Function-Help-tp25848627p25848627.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] function help
Dear Alex, Just a _minor_ change in the function "powers": # powers # n is the number of samples # DATA is the ORIGINAL data set powers=function(n,m2,n2,s2,DATA){ outp=Sample(DATA,n) # DATA was 'name' before, which doesn't make sense mymeans=outp$Gmean mysds=outp$Gsd res=t(rbind(mymeans,mysds,n=n)) apply(res,1,Power,m2=m2,n2=n2,s2=s2) } HTH, Jorge On Fri, Oct 17, 2008 at 4:01 PM, Jorge Ivan Velez <[EMAIL PROTECTED]>wrote: > > Dear Alex, > Try this: > > > > # Data > name=read.table(textConnection(" > X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 > 0 1 000100000000 > 0 0 001000000010 > 0 1 000000000010 > 1 0 00100001000 > 0"),header=TRUE) > closeAllConnections() > rownames(name)=paste('s',1:4,sep="") > > # Sample > # Function to estimate both the big mean and big standard deviation > # name is the data set > # n is the number of samples > Sample=function(name, n){ > res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE) > Means=do.call(rbind,lapply(res,function(x) rowMeans(x))) > rownames(Means)=paste('sample',1:n,sep="") > Gmean=colMeans(Means) > STDs=do.call(rbind,lapply(res,function(x) apply(x,1,sd))) > rownames(STDs)=rownames(Means) > Gsd=sd(STDs) > list(Gmean=Gmean,Gsd=Gsd) > } > > # Power > # mypars is a vector of length three which components > # are n1, s1 and n1 > # m2, s2 and n2 may change > # alfa is "fixed" > Power=function(mypars,m2,s2,n2,alfa=0.05){ > m1=mypars[1] > s1=mypars[2] > n1=mypars[3] > up=abs(m1-m2) > down=sqrt(((s1^2)/(n1))+((s2^2)/(n2))) > z=(-1)*(qnorm(1-(alfa/2))) > fi=z+(up/down) > pow=pnorm(fi) > return(pow) > } > > # powers > # n is the number of samples > # DATA is the ORIGINAL data set > powers=function(n,m2,n2,s2,DATA){ > outp=Sample(name,n) > mymeans=outp$Gmean > mysds=outp$Gsd > res=t(rbind(mymeans,mysds,n=n)) > apply(res,1,Power,m2=m2,n2=n2,s2=s2) > } > > > # ten samples and values for m2, n2 and s2 > powers(n=10,m2=0.12,n2=10,s2=0.15) > > > HTH, > > Jorge > > > On Fri, Oct 17, 2008 at 11:50 AM, Alex99 <[EMAIL PROTECTED]> wrote: > >> >> Hi everyone, >> I have dataset which I make a sample of it couple of times and each time I >> get the mean and standard deviation of each row for each sample. I have a >> function for that, which takes the name of the file and number of times to >> sample and then returns the mean and standard deviation for each row in >> each >> sample. >> >> Sample=function(name, n){ >> res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE) >> >> Means=do.call(rbind,lapply(res,function(x) rowMeans(x))) >> rownames(Means)=paste('sample',1:n,sep="") >> Gmean=colMeans(Means) >> >> STDs=do.call(rbind,lapply(res,function(x) rowSds(x))) >> rownames(STDs)=paste('sample',1:5,sep="") >> Gsd=sd(STDs) >> >> return(Gmean,Gsd)} >> >> I then need to use the mean and standard deviation from each row to >> calculate the Power for 2 sample means. here is the function I use to do >> it: >> >> Power=function(alfa,m1,m2,s1,s2,n1,n2){ >> up=abs(m1-m2) >> down=sqrt(((s1^2)/(n1))+((s2^2)/(n2))) >> z=(-1)*(qnorm(1-(alfa/2))) >> fi=z+(up/down) >> pow=pnorm(fi) >> return(pow)} >> >> then I need to call the Power function: >> >> Power(.05,57,mi,33,si,200,n) >> >> the alfa,m1,s1 and n1 values are constant but the m2,s2 and n2 values are >> changing. >> n2 is the n used in sample function(which I could input manually) >> m2 should be the grand mean for each row >> s2 is the grand standard deviation for each row >> >> example:this is a sample output for "sample" function: >> $Gmean >> s1 s2 s3 s4 >> 0.08 0.20 0.12 0.20 >> >> $Gsd >> s1s2s3s4 >> 0.2449490 0.2156741 0.2449490 0.2156741 >> my problem is how can I put the corresponding means and standard >> devotions >> together in the power function?I used a for loop, but the problem is >> getting the s1...s4 from Gmean and Gsd >> >> Thanks for your help >> >> >> -- >> View this message in context: >> http://www.nabble.com/function-help-tp20035949p20035949.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> R-help@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. >> > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] function help
Dear Alex, Try this: # Data name=read.table(textConnection(" X8 X9 X10 X102 X110 X177 X283 X284 X286 X292 X297 X306 X308 X314 0 1 000100000000 0 0 001000000010 0 1 000000000010 1 0 00100001000 0"),header=TRUE) closeAllConnections() rownames(name)=paste('s',1:4,sep="") # Sample # Function to estimate both the big mean and big standard deviation # name is the data set # n is the number of samples Sample=function(name, n){ res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE) Means=do.call(rbind,lapply(res,function(x) rowMeans(x))) rownames(Means)=paste('sample',1:n,sep="") Gmean=colMeans(Means) STDs=do.call(rbind,lapply(res,function(x) apply(x,1,sd))) rownames(STDs)=rownames(Means) Gsd=sd(STDs) list(Gmean=Gmean,Gsd=Gsd) } # Power # mypars is a vector of length three which components # are n1, s1 and n1 # m2, s2 and n2 may change # alfa is "fixed" Power=function(mypars,m2,s2,n2,alfa=0.05){ m1=mypars[1] s1=mypars[2] n1=mypars[3] up=abs(m1-m2) down=sqrt(((s1^2)/(n1))+((s2^2)/(n2))) z=(-1)*(qnorm(1-(alfa/2))) fi=z+(up/down) pow=pnorm(fi) return(pow) } # powers # n is the number of samples # DATA is the ORIGINAL data set powers=function(n,m2,n2,s2,DATA){ outp=Sample(name,n) mymeans=outp$Gmean mysds=outp$Gsd res=t(rbind(mymeans,mysds,n=n)) apply(res,1,Power,m2=m2,n2=n2,s2=s2) } # ten samples and values for m2, n2 and s2 powers(n=10,m2=0.12,n2=10,s2=0.15) HTH, Jorge On Fri, Oct 17, 2008 at 11:50 AM, Alex99 <[EMAIL PROTECTED]> wrote: > > Hi everyone, > I have dataset which I make a sample of it couple of times and each time I > get the mean and standard deviation of each row for each sample. I have a > function for that, which takes the name of the file and number of times to > sample and then returns the mean and standard deviation for each row in > each > sample. > > Sample=function(name, n){ > res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE) > > Means=do.call(rbind,lapply(res,function(x) rowMeans(x))) > rownames(Means)=paste('sample',1:n,sep="") > Gmean=colMeans(Means) > > STDs=do.call(rbind,lapply(res,function(x) rowSds(x))) > rownames(STDs)=paste('sample',1:5,sep="") > Gsd=sd(STDs) > > return(Gmean,Gsd)} > > I then need to use the mean and standard deviation from each row to > calculate the Power for 2 sample means. here is the function I use to do > it: > > Power=function(alfa,m1,m2,s1,s2,n1,n2){ > up=abs(m1-m2) > down=sqrt(((s1^2)/(n1))+((s2^2)/(n2))) > z=(-1)*(qnorm(1-(alfa/2))) > fi=z+(up/down) > pow=pnorm(fi) > return(pow)} > > then I need to call the Power function: > > Power(.05,57,mi,33,si,200,n) > > the alfa,m1,s1 and n1 values are constant but the m2,s2 and n2 values are > changing. > n2 is the n used in sample function(which I could input manually) > m2 should be the grand mean for each row > s2 is the grand standard deviation for each row > > example:this is a sample output for "sample" function: > $Gmean > s1 s2 s3 s4 > 0.08 0.20 0.12 0.20 > > $Gsd > s1s2s3s4 > 0.2449490 0.2156741 0.2449490 0.2156741 > my problem is how can I put the corresponding means and standard devotions > together in the power function?I used a for loop, but the problem is > getting the s1...s4 from Gmean and Gsd > > Thanks for your help > > > -- > View this message in context: > http://www.nabble.com/function-help-tp20035949p20035949.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] function help
take a look at ?mapply(); for instance, you can use something like this (untested): dat <- Sample(...) mapply(Power, dat$Gmean, dat$Gsd, MoreArgs = list(alfa = 0.05, m1 = 57, s1 = 33, n1 = 200, n2 = 100)) I hope it helps. Best, Dimitris Alex99 wrote: Hi everyone, I have dataset which I make a sample of it couple of times and each time I get the mean and standard deviation of each row for each sample. I have a function for that, which takes the name of the file and number of times to sample and then returns the mean and standard deviation for each row in each sample. Sample=function(name, n){ res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE) Means=do.call(rbind,lapply(res,function(x) rowMeans(x))) rownames(Means)=paste('sample',1:n,sep="") Gmean=colMeans(Means) STDs=do.call(rbind,lapply(res,function(x) rowSds(x))) rownames(STDs)=paste('sample',1:5,sep="") Gsd=sd(STDs) return(Gmean,Gsd)} I then need to use the mean and standard deviation from each row to calculate the Power for 2 sample means. here is the function I use to do it: Power=function(alfa,m1,m2,s1,s2,n1,n2){ up=abs(m1-m2) down=sqrt(((s1^2)/(n1))+((s2^2)/(n2))) z=(-1)*(qnorm(1-(alfa/2))) fi=z+(up/down) pow=pnorm(fi) return(pow)} then I need to call the Power function: Power(.05,57,mi,33,si,200,n) the alfa,m1,s1 and n1 values are constant but the m2,s2 and n2 values are changing. n2 is the n used in sample function(which I could input manually) m2 should be the grand mean for each row s2 is the grand standard deviation for each row example:this is a sample output for "sample" function: $Gmean s1 s2 s3 s4 0.08 0.20 0.12 0.20 $Gsd s1s2s3s4 0.2449490 0.2156741 0.2449490 0.2156741 my problem is how can I put the corresponding means and standard devotions together in the power function?I used a for loop, but the problem is getting the s1...s4 from Gmean and Gsd Thanks for your help -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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] function help
Hi everyone, I have dataset which I make a sample of it couple of times and each time I get the mean and standard deviation of each row for each sample. I have a function for that, which takes the name of the file and number of times to sample and then returns the mean and standard deviation for each row in each sample. Sample=function(name, n){ res=replicate(n,name[,sample(colnames(name),n)],simplify=FALSE) Means=do.call(rbind,lapply(res,function(x) rowMeans(x))) rownames(Means)=paste('sample',1:n,sep="") Gmean=colMeans(Means) STDs=do.call(rbind,lapply(res,function(x) rowSds(x))) rownames(STDs)=paste('sample',1:5,sep="") Gsd=sd(STDs) return(Gmean,Gsd)} I then need to use the mean and standard deviation from each row to calculate the Power for 2 sample means. here is the function I use to do it: Power=function(alfa,m1,m2,s1,s2,n1,n2){ up=abs(m1-m2) down=sqrt(((s1^2)/(n1))+((s2^2)/(n2))) z=(-1)*(qnorm(1-(alfa/2))) fi=z+(up/down) pow=pnorm(fi) return(pow)} then I need to call the Power function: Power(.05,57,mi,33,si,200,n) the alfa,m1,s1 and n1 values are constant but the m2,s2 and n2 values are changing. n2 is the n used in sample function(which I could input manually) m2 should be the grand mean for each row s2 is the grand standard deviation for each row example:this is a sample output for "sample" function: $Gmean s1 s2 s3 s4 0.08 0.20 0.12 0.20 $Gsd s1s2s3s4 0.2449490 0.2156741 0.2449490 0.2156741 my problem is how can I put the corresponding means and standard devotions together in the power function?I used a for loop, but the problem is getting the s1...s4 from Gmean and Gsd Thanks for your help -- View this message in context: http://www.nabble.com/function-help-tp20035949p20035949.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.