Skip to content
Back to formatted view

Raw Message

Message-ID: <4DB80C4C.7060707@statistik.tu-dortmund.de>
Date: 2011-04-27T12:30:04Z
From: Uwe Ligges
Subject: Eval to write many files
In-Reply-To: <412311.19778.qm@web120112.mail.ne1.yahoo.com>

On 27.04.2011 13:59, Alaios wrote:
> Dear all
> I am looking for a shorter way and more elegant to write the following
>
>
> for (i in c(1:length(Shadowlist))){
>    filename<-paste('/home/apa/maps/',model,i,'.mat',sep="")
>    varname<-paste(model,'_shadow',i,sep="")
>    eval(parse(text=paste('writeMat(filename,',varname,'=Shadowlist[[i]])',sep="")))
> }


Won't be much shorter, but in loops use
seq_alonmg(Shadowlist) rather than c(1:length(Shadowlist))
In the latter, the c() is useless and 1:length(.) may become 1:0 for a 
length 0 object.

Actually I'd try:

     sa <- seq_along(Shadowlist)
     filenames <- paste('/home/apa/maps/', model, sa, '.mat', sep="")
     names(Shadowlist) <- paste(model, '_shadow', sa, sep="")
     for(i in sa)
         do.call(writeMat, c(con = filenames[i], Shadowlist[i]))


Uwe Ligges





> actually I do not like eval at the end but I wanted to control the varname inside the writeMat command
> writeMat(filename,varname=Shadowlist[[i]])
>
> ______________________________________________
> 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.