scope (was [R] URGENT Help required)
Liaw, Andy
andy_liaw at merck.com
Tue Nov 26 14:21:44 CET 2002
Please use a more meaningful subject. Some might take it as a spam and hit
the delete button.
Objects in the workspace are visible inside functions, but are usually not
directly manipulated inside functions. When you modify an object in the
workspace inside a function, what actually happens is a local copy is made,
reflecting the modifications you made, and leave the one in the workspace
untouched.
Here's a simple example:
> x <- 10
> f <- function() { print(x); x <- 5; print(x) }
> f()
[1] 10
[1] 5
> x
[1] 10
I suppose the "usual" way of doing what you wanted to do is make the objects
you want inside the function (rather than the workspace), and return a list
containing them. E.g., something like:
myFunction <- function(IndDF)
{
models <- matrix(nrow = 20, ncol =8)
X <- matrix(nrow = 1, ncol = 6)
compt <- 1
indDF <- as.data.frame(IndDF)
for (i in 2:(length(IndDF)-1))
{
[whatever you need to do...]
}
return(list(models = models, compt = compt))
}
Andy
-----Original Message-----
From: arnaud_amsellem at ssga.com [mailto:arnaud_amsellem at ssga.com]
Sent: Tuesday, November 26, 2002 6:29 AM
To: r-help at stat.math.ethz.ch
Subject: [R] URGENT Help required
I've the following problem:
The below function runs a loop with regression analysis and stores F-Stat
in a matrix. When I call the matrix elements (models[i,j]) in the function
I get proper results, but when I try to call the same elements outside the
function the matrix appears to be empty e.g when I call compt inside the
function I get: 5 but when I call it outside I get: 1. This is the same
problem with models which appears to be empty outside the function.
Specifically I would like being able to run the same function for various
data frames and store the results in the same matrix (models) one after the
other.
# Set general variables & matrix
myDataFrame <- read.table("path",header=T)
models <- matrix(nrow = 20, ncol =8)
X <- matrix(nrow = 1, ncol = 6)
compt <- 1
deb <- 0
fin <- 0
# Runsanalysis
myFunction <- function(IndDF)
{
indDF <- as.data.frame(IndDF)
for (i in 2:(length(IndDF)-1))
{
for (j in ((i+1):(length(IndDF))))
{
for (k in 0:5)
{
deb <- dim(IndDF)[1]-k*21-100
fin <- dim(IndDF)[1]-k*21
X[1,k+1] <- lm((IndDF[,i])[deb:fin]~(IndDF
[,j])[deb:fin]))$fstat
}
if (0.39*X[1,1]+0.25*X[1,2]+0.16*X[1,3]+0.10*X[1,4]
+0.06*X[1,5]+0.04*X[1,6] > 5)
{
models[compt,1] <- "ind"
models[compt,2] <-
substr(as.character(names(IndDF)[i]),2,7)
models[compt,3] <-
substr(as.character(names(IndDF)[j]),2,7)
models[compt,6] <- 0.39*X[1,1]+0.25*X[1,2]+0.16*X[1,3]
+0.10*X[1,4]+0.06*X[1,5]+0.04*X[1,6]
models[compt,7] <- as.character((IndDF
[,1])[dim(IndDF)[1]-100])
models[compt,8] <- as.character((IndDF
[,1])[dim(IndDF)[1]])
compt <- compt+1
}
}
}
}
Any help appreciated
Thanks
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.
==============================================================================
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list