An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130301/50d29f55/attachment.pl>
using reserved words in R, and reuse variable names in different functions
11 messages · C W, David Winsemius, Sarah Goslee +5 more
On Mar 1, 2013, at 1:56 PM, C W wrote:
Hi list,
I am writing several functions and running out variable names. I am using
words such as "t", "c", "matrix" to keep the notation same as formulas I am
using.
For example I have,
unnormalized <- function(t, x, y){
val <- rnorm(t, mean=x, var=y)
return(val)
}
metropolis <- function(t, c, x, y){
den1 <- unnormalized(t, mean=x, sd=y)
den2 <- unnormalized(c, mean=x, sd=y)
if(den1 < den2)
return(a)
else
return(b)
}
for(i in 1: 100){
matrix <- c()
matrix[i] <- metroplis(1, 2, 3, 4)
}
Here, I reused letter "t" and "c", and the word "matrix". Could this cause
any potential problems?
Whatever problems you are having at the moment (and they appear to be many) are not due to using existing function names as data-object names. You are asked to report the error messages you get with your code problems, and those messages I found to be reasonably informative for the first 5 errors I found.
David Winsemius Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130301/9a18a5d9/attachment.pl>
On Fri, Mar 1, 2013 at 7:06 PM, C W <tmrsg11 at gmail.com> wrote:
Thanks, that was just an example I came up with. I was just curious if using same variable names in different functions would cause problems.
No. The environment of a function is independent of other functions.
Especially with reserved words.
Yes. Using reserved words can cause all kinds of subtle problems. Avoid it. Sarah
Mike On Fri, Mar 1, 2013 at 5:45 PM, David Winsemius <dwinsemius at comcast.net>wrote:
On Mar 1, 2013, at 1:56 PM, C W wrote:
Hi list, I am writing several functions and running out variable names. I am
using
words such as "t", "c", "matrix" to keep the notation same as formulas I
am
using.
For example I have,
unnormalized <- function(t, x, y){
val <- rnorm(t, mean=x, var=y)
return(val)
}
metropolis <- function(t, c, x, y){
den1 <- unnormalized(t, mean=x, sd=y)
den2 <- unnormalized(c, mean=x, sd=y)
if(den1 < den2)
return(a)
else
return(b)
}
for(i in 1: 100){
matrix <- c()
matrix[i] <- metroplis(1, 2, 3, 4)
}
Here, I reused letter "t" and "c", and the word "matrix". Could this
cause
any potential problems?
Whatever problems you are having at the moment (and they appear to be many) are not due to using existing function names as data-object names. You are asked to report the error messages you get with your code problems, and those messages I found to be reasonably informative for the first 5 errors I found.
Sarah Goslee http://www.functionaldiversity.org
See fortune("dog").
To wit: "Firstly, don't call your matrix 'matrix'. Would you call your dog 'dog'? Anyway, it might clash with the function 'matrix'" I once had a cat named "kitty" and she never had a problem with it. Clashes between non-functions and functions that cause problems are not that common. With 4000 packages, each with a number of functions, it is hard to avoid using a name that someone has used for a function. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rolf Turner Sent: Saturday, March 02, 2013 5:01 AM To: Sarah Goslee Cc: r-help Subject: Re: [R] using reserved words in R, and reuse variable names in different functions On 03/02/2013 01:12 PM, Sarah Goslee wrote:
On Fri, Mar 1, 2013 at 7:06 PM, C W <tmrsg11 at gmail.com> wrote:
Thanks, that was just an example I came up with. I was just curious if using same variable names in different functions would cause problems.
No. The environment of a function is independent of other functions.
Especially with reserved words.
Yes. Using reserved words can cause all kinds of subtle problems. Avoid it.
Very sound advice. But it should be noted that "t", "c", and "matrix"
to which
the OP referred are *not* technically reserved words. Nonetheless their use
as names of user-defined objects should be eschewed. See fortune("dog").
You *can't* actually assign values to reserved words. E.g.
TRUE <- 42
throws an error. (Whereas matrix <- 42, bad form though it may be,
does not throw an error.)
cheers,
Rolf Turner
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130301/4eb74350/attachment.pl>
On 13-03-01 8:35 PM, C W wrote:
Thanks, everyone, I will definitely avoid it. Is there any tips on naming variables? I've seen the Google R style guider and Hadley R style guide.
Name them in ways that are meaningful to you. R standard function names are famous for not following any naming pattern consistently. Avoid using dots in the name unless you are defining methods (e.g. print.lm is the print method for lm objects).
For example, I want to use pie_t, to denote stationary distribution pie at time t. Both "pi" and "pie" are function names themselves.
Actually pi is not a function, but it is a variable. If you write a package that exports a function or variable named pi, it would mask the standard one, and that could cause big problems for users. If you use it internally, it will only mask the standard one in your code, and that may not matter to you. pie is a function, but all it does is draw pie charts, so who cares if you mask it? :-). Duncan Murdoch
Mike On Fri, Mar 1, 2013 at 8:12 PM, William Dunlap <wdunlap at tibco.com> wrote:
See fortune("dog").
To wit: "Firstly, don't call your matrix 'matrix'. Would you call your dog 'dog'? Anyway, it might clash with the function 'matrix'" I once had a cat named "kitty" and she never had a problem with it. Clashes between non-functions and functions that cause problems are not that common. With 4000 packages, each with a number of functions, it is hard to avoid using a name that someone has used for a function. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf
Of Rolf Turner Sent: Saturday, March 02, 2013 5:01 AM To: Sarah Goslee Cc: r-help Subject: Re: [R] using reserved words in R, and reuse variable names in
different
functions On 03/02/2013 01:12 PM, Sarah Goslee wrote:
On Fri, Mar 1, 2013 at 7:06 PM, C W <tmrsg11 at gmail.com> wrote:
Thanks, that was just an example I came up with. I was just curious
if
using same variable names in different functions would cause problems.
No. The environment of a function is independent of other functions.
Especially with reserved words.
Yes. Using reserved words can cause all kinds of subtle problems.
Avoid it.
Very sound advice. But it should be noted that "t", "c", and "matrix" to which the OP referred are *not* technically reserved words. Nonetheless their
use
as names of user-defined objects should be eschewed. See fortune("dog").
You *can't* actually assign values to reserved words. E.g.
TRUE <- 42
throws an error. (Whereas matrix <- 42, bad form though it may be,
does not throw an error.)
cheers,
Rolf Turner
______________________________________________ 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. ______________________________________________ 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.
[[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.
On 03/02/2013 01:12 PM, Sarah Goslee wrote:
On Fri, Mar 1, 2013 at 7:06 PM, C W <tmrsg11 at gmail.com> wrote:
Thanks, that was just an example I came up with. I was just curious if using same variable names in different functions would cause problems.
No. The environment of a function is independent of other functions.
Especially with reserved words.
Yes. Using reserved words can cause all kinds of subtle problems. Avoid it.
Very sound advice. But it should be noted that "t", "c", and "matrix"
to which
the OP referred are *not* technically reserved words. Nonetheless their use
as names of user-defined objects should be eschewed. See fortune("dog").
You *can't* actually assign values to reserved words. E.g.
TRUE <- 42
throws an error. (Whereas matrix <- 42, bad form though it may be,
does not throw an error.)
cheers,
Rolf Turner
Duncan's comment may not qualify as a fortune, but it did make me chuckle. Peter Ehlers
On 2013-03-02 03:01, Duncan Murdoch wrote:
On 13-03-01 8:35 PM, C W wrote:
[...snip...]
pie is a function, but all it does is draw pie charts, so who cares if you mask it? :-). Duncan Murdoch
[...snip...]
Well *I* think it should be a fortune!
cheers,
Rolf
On 03/03/2013 10:30 AM, Peter Ehlers wrote:
Duncan's comment may not qualify as a fortune, but it did make me chuckle. Peter Ehlers On 2013-03-02 03:01, Duncan Murdoch wrote:
On 13-03-01 8:35 PM, C W wrote:
[...snip...]
pie is a function, but all it does is draw pie charts, so who cares if you mask it? :-). Duncan Murdoch
[...snip...]
1 day later
Yes, it can cause problems. And speaking for myself, I'd say it's not worth the risk, because it's easy enough to find alternative variable names that are close enough to the notation of your formulas that remembering should be no problem. For example, "tt", "cc", and "mmatrix" might do it. -Don
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 3/1/13 1:56 PM, "C W" <tmrsg11 at gmail.com> wrote:
Hi list,
I am writing several functions and running out variable names. I am using
words such as "t", "c", "matrix" to keep the notation same as formulas I am
using.
For example I have,
unnormalized <- function(t, x, y){
val <- rnorm(t, mean=x, var=y)
return(val)
}
metropolis <- function(t, c, x, y){
den1 <- unnormalized(t, mean=x, sd=y)
den2 <- unnormalized(c, mean=x, sd=y)
if(den1 < den2)
return(a)
else
return(b)
}
for(i in 1: 100){
matrix <- c()
matrix[i] <- metroplis(1, 2, 3, 4)
}
Here, I reused letter "t" and "c", and the word "matrix". Could this cause
any potential problems?
Thanks in advance,
Mike
[[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.