[R] Changing global variables from functions
jtouyz at uwaterloo.ca
Tue Mar 16 19:43:37 CET 2010
Thank you for your response.
Yes, num_decks was previously defined in my program, I apologize for the
confusion as it is an integral value.
This is only one portion of my program that is being used to simulate card
counting in blackjack.
The basic idea was to modify deckn without having to return a value from the
function deck(). That is whenever deck() was run it would write over
whatever was in deckn (instead of having to input deckn<-deck().)
It seems from your response that this may not be possible and "return" is
If I have erred, please let me know.
Thanks once again,
David Winsemius wrote:
> On Mar 16, 2010, at 11:04 AM, jtouyz wrote:
>> Hey all,
>> I'm relatively new to the R-environment. I'm having a bit of trouble
>> I have a globally declared variable that doesn't update it when I
>> change it
>> in a function.
>> For example when I run the following function
>>> deck1<-1 #52 card deck
>> #Creating a standard deck
>> #Creating n decks
>> for (i in 2:num_decks)
> # could be wrong but it appears that you are expecting the act of
> putting "num_" in front of "decks" to be interpreted by R as returning
> the length of deckn or deck1. That is a higher level of abstraction
> than is yet available in any computer language with which I am
> familiar. Or perhaps you were intending to use Greg Snow's soon to be
> released mind-reading package so that R could know that you wanted it
> to be 6?.
> Perhaps instead (depending on what the real problem (unstated as yet)
> might be:
> for (i in 2:length(deck1) ) # or some other object or function
> that returns a numeric value.
> You would have needed to assign a "return"-ed value to deckn in the
> outer environment. The deckn object would have disappeared at the end
> of the function call, and it would not have needed to be named
> "deckn", either.
> Try instead:
> #Creating a standard deck
> #Creating n decks
> for (i in 2:num_decks)
> }; return(deckn)
> deckn <- deck()
> Which has a gap between 52 and 104 because of your logic, not mine.
>> it returns NULL for deckn instead of a vector of values. Is there an
>> fix to update deckn in the function so that it outputs a vector of
>> values (
>> I don't wish the function to return a value just update the current
> You could, of course, explain what you are trying to do.
>> Thanks in advance,
>> Josh Elliott
> David Winsemius, MD
> West Hartford, CT
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
View this message in context: http://n4.nabble.com/Changing-global-variables-from-functions-tp1595002p1595356.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help