[R] Changing global variables from functions
David Winsemius
dwinsemius at comcast.net
Tue Mar 16 20:42:26 CET 2010
On Mar 16, 2010, at 3:11 PM, David Winsemius wrote:
>
> On Mar 16, 2010, at 2:43 PM, jtouyz wrote:
>
>>
>> Hi David,
>> 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
>> always required.
>> If I have erred, please let me know.
>
> R is "supposed" to be used as a functional language. The use of the
> "<<-" operator is deprecated, so I did not mention that strategy.
> You could also have simply run the loop without the functional
> wrapper.
I wondered if a sampling without replacement strategy might work to
provide with indexing:
num_deck # what fills shoes these days.
(> table((1:52)[sample(1:(52*num_decks), 52*num_decks) %% 52])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
6 6 6 6 6 6 6
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
--
David.
>
> --
> david.
>>
>> Thanks once again,
>> Josh Elliott
>>
>>
>> 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
>>>> with
>>>> encapsulation.
>>>> 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
>>>>
>>>>> deckn<-NULL
>>>>> deck1<-1 #52 card deck
>>>>> deck<-function()
>>>> {
>>>> #Creating a standard deck
>>>> deck1<-c(1:52)
>>>> deckn<-deck1
>>>> #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.
>>>
>>>> {
>>>> deckn<-c(deckn,52*i+deck1-1)
>>>
>>>> }
>>>> }
>>>>> deckn
>>>
>>> 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:
>>>
>>> deckn<-NULL
>>> deck<-function(num_decks=6)
>>> {
>>> #Creating a standard deck
>>> deck1<-c(1:52)
>>> deckn<-deck1
>>> #Creating n decks
>>> for (i in 2:num_decks)
>>> {
>>> deckn<-c(deckn,52*i+deck1-1)
>>> }; return(deckn)
>>> }
>>> deckn <- deck()
>>> deckn
>>>
>>> Which has a gap between 52 and 104 because of your logic, not mine.
>>>
>>>
>>>>> NULL
>>>>
>>>> it returns NULL for deckn instead of a vector of values. Is there
>>>> an
>>>> easy
>>>> 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
>>>> one)?
>>>
>>> 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
>>> 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.
>>>
>>>
>>
>> --
>> 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.
>>
>> ______________________________________________
>> 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, MD
> West Hartford, CT
>
> ______________________________________________
> 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, MD
West Hartford, CT
More information about the R-help
mailing list