You need to assign the
parameters of your function. I suppose you want to calculate the new values
based on values in existing dataframe. So you need to pass the dataframe to the
function and then return the calculated values.
my.function <- function(my.dataframe)
{
Income <- new.values.from.some.calculation
return(Income)
}
my.dataframe <- data.frame(state.x77, row.names=state.abb))
my.dataframe$Income <-
my.function(my.dataframe)
Cheers,
Thierry
Van:
s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu]
Namens Jordão, Felipe
Verzonden: donderdag 18 augustus
2005 17:08
Aan:
s-news@lists.biostat.wustl.edu
Onderwerp: [S] Question about
assigning & scope
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.