[R] how to prevent duplications of data within a loop

David Freedman 3.14david at gmail.com
Sat Jan 24 18:46:22 CET 2009


or how about
lm(myData$response~as.matrix(myData[2:4]))

hth, david

Juliet Hannah wrote:
> 
> Hi All,
> 
> I had posted a question on a similar topic, but I think it was not
> focused. I am posting a modification that I think better accomplishes
> this.
> I hope this is ok, and I apologize if it is not. :)
> 
> I am looping through variables and running several regressions. I have
> reason to believe that the data is being duplicated because I have
> been
> monitoring the memory use on unix.
> 
> How can I avoid this? Here is an example of how I am going about this.
> For lm, I also tried model=FALSE, but this did not seem to do the job.
> Any ideas?
> Thanks for your time.
> 
> Regards,
> 
> Juliet
> 
> # create data
> set.seed(1)
> response <- rnorm(50)
> var1 <- rnorm(50)
> var2 <- rnorm(50)
> var3 <- rnorm(50)
> myData <- data.frame(response,var1,var2,var3)
> var.names <- names(myData)[2:4]
> 
> 
> numVars <- length(var.names)
> betas <- rep(-1,numVars)
> names(betas) <- var.names
> 
> #run regression on var1 through var3.
> 
> for (Var_num in 1:numVars)
> {
>   col.name <- var.names[Var_num]
>   mylm <- lm(response ~ get(col.name),data=myData,model=FALSE)
>   betas[Var_num] <- coef(mylm)[2]
> }
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-prevent-duplications-of-data-within-a-loop-tp21637431p21642232.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list