[R] Name assignment in for loop

nrm2010 nrm2010 at zoho.com
Sat Nov 10 18:38:00 CET 2012


Thank you, Bert and Peter, for helpful responses.  I'm having a little trouble with Bert's approach because writing the lapply function is challenging when I'm drawing from two dataframes.  Peter's approach works perfectly, although it has less "R" personality.   Wrapping the model statement in  try() keeps it running when individual models fail.

Cheers,

Toby

 > 
>lmModels <- vector('list', nrow(dat2)) 
>for (i in 1:nrow(dat2)) 
>{ 
> lmModels[[i]] <- etc 
>} 
> 
>Generally, I add a line after creating the list to name the elements and then address the list using these names as I find that less error prone and more informative. 
> 
>HTH .... 
> 
>Peter Alspach 
> 
>-----Original Message----- 
>From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of nrm2010 
>Sent: Friday, 2 November 2012 10:16 a.m. 
>To: r-help at r-project.org 
>Subject: [R] Name assignment in for loop 
> 
> 
> 
>Dear helpeRs- 
> 
>I'm using a for loop to create a series of models. 
>I'm trying to assign a name to each model created, using the loop index. The loop gets stuck at the name of the model, giving the error "target of assignment expands to non-language object". The linear model runs without error; only the name is problematic. 
> 
>Here is the current loop syntax. The use of dat and dat2 is not an error. I'm pulling data from 2 sources for the model. 
> 
>for (i in 1:dim(dat2)[[1]]) { 
>assign("modelb",i) = dat2$day1[i] & dat$doy <= dat2$day2[i]) dat2$coef[i] <- coef(assign("modelb",i, sep = ""))[[2]] dat2$Rsq[i] <- summary(assign("modelb",i, sep = ""))[[9]] } 
> 
>I have also tried 
>assign("modelb",1:i) #following the ?assign example paste("modelb", i, sep = "") <- (...) assign(paste("modelb", i, sep = "")) <- (...) assign(paste("modelb", i, sep - ""), put linear model here) They all generate the same error message. 
> 
> 
>dim(dat2)[[1]] is 29 
>> dim(dat2)[[1]] 
>[1] 29 
> 
>> class(dim(dat2)[[1]]) 
>[1] "integer" 
> 
>I have not included data because the problem is with the naming syntax; no data are involved except for the number 29. 
> 
>Given this approach, rather than the sapply() approach, what is the correct syntax for naming each model in the sequence? 
> 
>Thank you in advance. 
> 
>Toby Gass 
> 
>______________________________________________ 
>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. 
> 
>The contents of this e-mail are confidential and may be subject to legal privilege. 
> If you are not the intended recipient you must not use, disseminate, distribute or 
> reproduce all or any part of this e-mail or attachments. If you have received this 
> e-mail in error, please notify the sender and delete all material pertaining to this 
> e-mail. Any opinion or views expressed in this e-mail are those of the individual 
> sender and may not represent those of The New Zealand Institute for Plant and 
> Food Research Limited. 
>




More information about the R-help mailing list