s-news
[Top] [All Lists]

Re: Question about assigning & scope

To: Felipe <Felipe.Jordao@moodys.com>
Subject: Re: Question about assigning & scope
From: Tony Plate <tplate@blackmesacapital.com>
Date: Thu, 18 Aug 2005 09:45:48 -0600
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <CDBE3ADAFCB39F41A19FF2BE650978FF01C3275C@mdynycmsx03.ad.moodys.net>
References: <CDBE3ADAFCB39F41A19FF2BE650978FF01C3275C@mdynycmsx03.ad.moodys.net>
User-agent: Mozilla Thunderbird 1.0.5 (Windows/20050711)
Modifying global variables from within functions is not considered good S-programming style and is generally discouraged, for good reasons.

The standard S way to do something like this is to have the function return the modified data frame:

my.function <- function(df) {
    df$Income <- new.values.from.some.calculation
    return(df)
}

then:

> my.data.frame <- my.function(my.data.frame)

Another way would be to use a script file and not a function

file "my.script.q" contains one line:
my.dataframe$Income <- new.values.from.some.calculation

then:

> source("my.script.q")

Finally, if you really must have a function modify a global variable, you generally need to make a local copy of it first:

my.function <- function() {
    df <- my.data.frame
    df$Income <- new.values.from.some.calculation
    assign("my.data.frame", df, where=1)
}

hope this helps,

Tony Plate

Jordão wrote:
Hi all,

I’m having a bit of difficulty understanding this scope business. I’ve tried going through the help and googling to no avail. It is probably very simple for you S veterans out there.

What I would like to do is modify a column in a global data frame from within a function. For example, the current code I have looks like this and works as I want it to:

my.dataframe <- data.frame(state.x77, row.names=state.abb)

my.dataframe$Income <- new.values.from.some.calculation

I want to change it so it’s like this:

my.function <- function() {

       my.dataframe$Income <- new.values.from.some.calculation

}

When I call my.function, I get a message saying that I need to assign my.dataframe locally before replacement. I tried using code like this

my.function <- function() {

       assign("my.dataframe[,\"Income\"]", new.values.from.some.calculation)

}

but it also didn’t work. I don’t get an error message, but the Income column is not changed. The data I’m working with is big, so it’s not efficient to pass it into the function, change it, then return it.

thanks,
Felipe



---------------------------------------

The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Thank you.

Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.


<Prev in Thread] Current Thread [Next in Thread>