Can someone tell me a proper call to a procedure, in this case, pnorm.
In what follows, I had expected a = b, but they are not equal. What are wrong with first call and second call? Thank you! try<-function(x,log.p=FALSE){ a<-pnorm(x,log.p) # first call b<-pnorm(x,log.p=log.p) # second call list(a=a,b=b) } try(x=1.2,log.p=TRUE)$a try(x=1.2,log.p=TRUE)$b ______________________________________________ [hidden email] 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. |
Hello,
You are making a confusion between 1. the formal argument log.p 2. the variable log.p In the function body, log.p is a variable that exists in the function's frame, not the formal argument of pnorm. The first and the 3rd calls that follow output the same value. try(x = 1.2, log.p = TRUE)$a try(x = 1.2, log.p = TRUE)$b try(x = 1.2, 1)$a This is because in the function a<-pnorm(x,log.p) # first call passes log.p as the *second* argument, not as a value for pnorm's formal argument log.p. Unless when named, the arguments are passed in the order they appear in the function's definition: pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) and that becomes a<-pnorm(x,TRUE) # first call a<-pnorm(x,1) # first call, coerced to numeric. Let me give another example. In the function that follows the default is z = FALSE. In the first call the name z is not the name of the argument, it's the name of a variable that exists in the .GlobalEnv. In the second call, z = z assign the formal argument z the value of the variable z. f <- function(x, y = 0, z = FALSE){ a <- x b <- y d <- z list(a = a, b = b, d = d) } z <- 2 f(1, z) f(1, z = z) Hope this helps, Rui Barradas Às 11:11 de 20/09/20, Steven Yen escreveu: > Can someone tell me a proper call to a procedure, in this case, pnorm. > In what follows, I had expected a = b, but they are not equal. What are > wrong with first call and second call? Thank you! > > try<-function(x,log.p=FALSE){ > a<-pnorm(x,log.p) # first call > b<-pnorm(x,log.p=log.p) # second call > list(a=a,b=b) > } > > try(x=1.2,log.p=TRUE)$a > try(x=1.2,log.p=TRUE)$b > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
Thanks. So, to be safe, always a good idea to give the argument, e.g.,
q=1.96, log.p=FALSE, skipping mean=0 and sd=1 if not needed. Thanks. pnorm(q=1.96, log.p = FALSE) On 2020/9/20 下午 06:36, Rui Barradas wrote: > Hello, > > You are making a confusion between > > 1. the formal argument log.p > 2. the variable log.p > > In the function body, log.p is a variable that exists in the > function's frame, not the formal argument of pnorm. > The first and the 3rd calls that follow output the same value. > > try(x = 1.2, log.p = TRUE)$a > try(x = 1.2, log.p = TRUE)$b > try(x = 1.2, 1)$a > > This is because in the function > > a<-pnorm(x,log.p) # first call > > passes log.p as the *second* argument, not as a value for pnorm's > formal argument log.p. Unless when named, the arguments are passed in > the order they appear in the function's definition: > > pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) > > and that becomes > > a<-pnorm(x,TRUE) # first call > a<-pnorm(x,1) # first call, coerced to numeric. > > > Let me give another example. In the function that follows the default > is z = FALSE. > > In the first call the name z is not the name of the argument, it's the > name of a variable that exists in the .GlobalEnv. > > In the second call, z = z assign the formal argument z the value of > the variable z. > > > f <- function(x, y = 0, z = FALSE){ > a <- x > b <- y > d <- z > list(a = a, b = b, d = d) > } > z <- 2 > f(1, z) > f(1, z = z) > > > Hope this helps, > > Rui Barradas > > Às 11:11 de 20/09/20, Steven Yen escreveu: >> Can someone tell me a proper call to a procedure, in this case, >> pnorm. In what follows, I had expected a = b, but they are not equal. >> What are wrong with first call and second call? Thank you! >> >> try<-function(x,log.p=FALSE){ >> a<-pnorm(x,log.p) # first call >> b<-pnorm(x,log.p=log.p) # second call >> list(a=a,b=b) >> } >> >> try(x=1.2,log.p=TRUE)$a >> try(x=1.2,log.p=TRUE)$b >> >> ______________________________________________ >> [hidden email] 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]] ______________________________________________ [hidden email] 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. |
Hi Steven: Rui's detailed explanation was great. The way I think of it is,
if you don't want to send the variables in with the same order as the formal arguments, then you better name them as you send them in. On Sun, Sep 20, 2020 at 7:23 AM Steven Yen <[hidden email]> wrote: > Thanks. So, to be safe, always a good idea to give the argument, e.g., > q=1.96, log.p=FALSE, skipping mean=0 and sd=1 if not needed. Thanks. > > pnorm(q=1.96, log.p = FALSE) > > On 2020/9/20 下午 06:36, Rui Barradas wrote: > > Hello, > > > > You are making a confusion between > > > > 1. the formal argument log.p > > 2. the variable log.p > > > > In the function body, log.p is a variable that exists in the > > function's frame, not the formal argument of pnorm. > > The first and the 3rd calls that follow output the same value. > > > > try(x = 1.2, log.p = TRUE)$a > > try(x = 1.2, log.p = TRUE)$b > > try(x = 1.2, 1)$a > > > > This is because in the function > > > > a<-pnorm(x,log.p) # first call > > > > passes log.p as the *second* argument, not as a value for pnorm's > > formal argument log.p. Unless when named, the arguments are passed in > > the order they appear in the function's definition: > > > > pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) > > > > and that becomes > > > > a<-pnorm(x,TRUE) # first call > > a<-pnorm(x,1) # first call, coerced to numeric. > > > > > > Let me give another example. In the function that follows the default > > is z = FALSE. > > > > In the first call the name z is not the name of the argument, it's the > > name of a variable that exists in the .GlobalEnv. > > > > In the second call, z = z assign the formal argument z the value of > > the variable z. > > > > > > f <- function(x, y = 0, z = FALSE){ > > a <- x > > b <- y > > d <- z > > list(a = a, b = b, d = d) > > } > > z <- 2 > > f(1, z) > > f(1, z = z) > > > > > > Hope this helps, > > > > Rui Barradas > > > > Às 11:11 de 20/09/20, Steven Yen escreveu: > >> Can someone tell me a proper call to a procedure, in this case, > >> pnorm. In what follows, I had expected a = b, but they are not equal. > >> What are wrong with first call and second call? Thank you! > >> > >> try<-function(x,log.p=FALSE){ > >> a<-pnorm(x,log.p) # first call > >> b<-pnorm(x,log.p=log.p) # second call > >> list(a=a,b=b) > >> } > >> > >> try(x=1.2,log.p=TRUE)$a > >> try(x=1.2,log.p=TRUE)$b > >> > >> ______________________________________________ > >> [hidden email] 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]] > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] 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. |
Argument passing is fundamental, even more so when you write your own
functions, which any half-serious R user will want to do. What has heretofore been discussed in this thread is not the whole story (e.g. there are ... arguments and functions as binary operators, among other things). See section 10 of the "Introduction to R" document that ships with R or any other decent R tutorial of your choice. The R language definition is the definitive reference (section 4 especially for this). All imo of course. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 20, 2020 at 8:57 AM Mark Leeds <[hidden email]> wrote: > Hi Steven: Rui's detailed explanation was great. The way I think of it is, > if you don't > want to send the variables in with the same order as the formal > arguments, then you > better name them as you send them in. > > > > > > On Sun, Sep 20, 2020 at 7:23 AM Steven Yen <[hidden email]> wrote: > > > Thanks. So, to be safe, always a good idea to give the argument, e.g., > > q=1.96, log.p=FALSE, skipping mean=0 and sd=1 if not needed. Thanks. > > > > pnorm(q=1.96, log.p = FALSE) > > > > On 2020/9/20 下午 06:36, Rui Barradas wrote: > > > Hello, > > > > > > You are making a confusion between > > > > > > 1. the formal argument log.p > > > 2. the variable log.p > > > > > > In the function body, log.p is a variable that exists in the > > > function's frame, not the formal argument of pnorm. > > > The first and the 3rd calls that follow output the same value. > > > > > > try(x = 1.2, log.p = TRUE)$a > > > try(x = 1.2, log.p = TRUE)$b > > > try(x = 1.2, 1)$a > > > > > > This is because in the function > > > > > > a<-pnorm(x,log.p) # first call > > > > > > passes log.p as the *second* argument, not as a value for pnorm's > > > formal argument log.p. Unless when named, the arguments are passed in > > > the order they appear in the function's definition: > > > > > > pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) > > > > > > and that becomes > > > > > > a<-pnorm(x,TRUE) # first call > > > a<-pnorm(x,1) # first call, coerced to numeric. > > > > > > > > > Let me give another example. In the function that follows the default > > > is z = FALSE. > > > > > > In the first call the name z is not the name of the argument, it's the > > > name of a variable that exists in the .GlobalEnv. > > > > > > In the second call, z = z assign the formal argument z the value of > > > the variable z. > > > > > > > > > f <- function(x, y = 0, z = FALSE){ > > > a <- x > > > b <- y > > > d <- z > > > list(a = a, b = b, d = d) > > > } > > > z <- 2 > > > f(1, z) > > > f(1, z = z) > > > > > > > > > Hope this helps, > > > > > > Rui Barradas > > > > > > Às 11:11 de 20/09/20, Steven Yen escreveu: > > >> Can someone tell me a proper call to a procedure, in this case, > > >> pnorm. In what follows, I had expected a = b, but they are not equal. > > >> What are wrong with first call and second call? Thank you! > > >> > > >> try<-function(x,log.p=FALSE){ > > >> a<-pnorm(x,log.p) # first call > > >> b<-pnorm(x,log.p=log.p) # second call > > >> list(a=a,b=b) > > >> } > > >> > > >> try(x=1.2,log.p=TRUE)$a > > >> try(x=1.2,log.p=TRUE)$b > > >> > > >> ______________________________________________ > > >> [hidden email] 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]] > > > > ______________________________________________ > > [hidden email] 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]] > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] 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. |
Thanks to all for educating me about procedures and argument. Those were
very helpful!! On 2020/9/21 上午 12:26, Bert Gunter wrote: > Argument passing is fundamental, even more so when you write your own > functions, which any half-serious R user will want to do. What has > heretofore been discussed in this thread is not the whole story (e.g. > there are ... arguments and functions as binary operators, among other > things). See section 10 of the "Introduction to R" document that > ships with R or any other decent R tutorial of your choice. The R > language definition is the definitive reference (section 4 especially > for this). > > All imo of course. > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Sun, Sep 20, 2020 at 8:57 AM Mark Leeds <[hidden email] > <mailto:[hidden email]>> wrote: > > Hi Steven: Rui's detailed explanation was great. The way I think > of it is, > if you don't > want to send the variables in with the same order as the formal > arguments, then you > better name them as you send them in. > > > > > > On Sun, Sep 20, 2020 at 7:23 AM Steven Yen <[hidden email] > <mailto:[hidden email]>> wrote: > > > Thanks. So, to be safe, always a good idea to give the argument, > e.g., > > q=1.96, log.p=FALSE, skipping mean=0 and sd=1 if not needed. Thanks. > > > > pnorm(q=1.96, log.p = FALSE) > > > > On 2020/9/20 下午 06:36, Rui Barradas wrote: > > > Hello, > > > > > > You are making a confusion between > > > > > > 1. the formal argument log.p > > > 2. the variable log.p > > > > > > In the function body, log.p is a variable that exists in the > > > function's frame, not the formal argument of pnorm. > > > The first and the 3rd calls that follow output the same value. > > > > > > try(x = 1.2, log.p = TRUE)$a > > > try(x = 1.2, log.p = TRUE)$b > > > try(x = 1.2, 1)$a > > > > > > This is because in the function > > > > > > a<-pnorm(x,log.p) # first call > > > > > > passes log.p as the *second* argument, not as a value for pnorm's > > > formal argument log.p. Unless when named, the arguments are > passed in > > > the order they appear in the function's definition: > > > > > > pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) > > > > > > and that becomes > > > > > > a<-pnorm(x,TRUE) # first call > > > a<-pnorm(x,1) # first call, coerced to numeric. > > > > > > > > > Let me give another example. In the function that follows the > default > > > is z = FALSE. > > > > > > In the first call the name z is not the name of the argument, > it's the > > > name of a variable that exists in the .GlobalEnv. > > > > > > In the second call, z = z assign the formal argument z the > value of > > > the variable z. > > > > > > > > > f <- function(x, y = 0, z = FALSE){ > > > a <- x > > > b <- y > > > d <- z > > > list(a = a, b = b, d = d) > > > } > > > z <- 2 > > > f(1, z) > > > f(1, z = z) > > > > > > > > > Hope this helps, > > > > > > Rui Barradas > > > > > > Às 11:11 de 20/09/20, Steven Yen escreveu: > > >> Can someone tell me a proper call to a procedure, in this case, > > >> pnorm. In what follows, I had expected a = b, but they are > not equal. > > >> What are wrong with first call and second call? Thank you! > > >> > > >> try<-function(x,log.p=FALSE){ > > >> a<-pnorm(x,log.p) # first call > > >> b<-pnorm(x,log.p=log.p) # second call > > >> list(a=a,b=b) > > >> } > > >> > > >> try(x=1.2,log.p=TRUE)$a > > >> try(x=1.2,log.p=TRUE)$b > > >> > > >> ______________________________________________ > > >> [hidden email] <mailto:[hidden email]> 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]] > > > > ______________________________________________ > > [hidden email] <mailto:[hidden email]> 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]] > > ______________________________________________ > [hidden email] <mailto:[hidden email]> 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]] ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |