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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 26 Nov 2002 arnaud_amsellem at ssga.com wrote: have you tried return(compt) as the last line of your function and catch the result of your function? your compt inside the function is local not global and thus different from the compt outside the function if you need global assignment have a look at assign() or <<- JN %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 %_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ % *********************************************************************** Jens Nieschulze Institute for Forest Biometrics & Phone: ++49-551-39-12107 Applied Computer Science Fax : ++49-551-39-3465 Buesgenweg 4 37077 Goettingen E-mail: jniesch at uni-forst.gwdg.de GERMANY http://www.uni-forst.gwdg.de/~jniesch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
arnaud_amsellem at ssga.com wrote:> > 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.[code snipped] Well, beside this remark on the completely nonsensical subject of this message, I'd suggest to read the manual "An Introduction to R" or other introductory literature. Please try to understand the scoping rules. You need to return() values from the function, in any other case the environment the function is executed in will be deleted after execution (this summary is too short, but I don't want to rewrite existing literature). Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._