Thanks Tony & Thierry, you have given me enough info to proceed with what I
wanted to do.
Regards,
Felipe
-----Original Message-----
From: Tony Plate [mailto:tplate@blackmesacapital.com]
Sent: Thursday, 18 August, 2005 11:46
To: Jordão, Felipe
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] Question about assigning & scope
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.
|