[R] save in for loop

Joris Meys jorismeys at gmail.com
Fri Jun 4 12:46:16 CEST 2010


On a side note:

On Thu, May 20, 2010 at 9:43 AM, Ivan Calandra
<ivan.calandra at uni-hamburg.de> wrote:
> Thanks to all of you for your answers!
>
> ...
>
> Tao, I don't understand why you have backslashes before "file" and after
> .rda. I guess it's something about regular expression, but I'm still
> very new to it.
> eval(parse(text=paste("save(file", i, ", file=\"file", i, ".rda\")",
> sep="")))
>
Very simple: You need to give a command as a string. In the save
command, you have to put quotation marks around the filename. Now
within the paste function, a simple quotation mark would make R
believe the string to paste ends there, and you don't want that. So
you escape the " by typing \", then R knows you want to add the symbol
" to the string instead of end it.  :

> paste("save(file", i, ", file=\"file", i, ".rda\")",sep="")
[1] "save(file2, file=\"file2.rda\")"

> parse(text=paste("save(file", i, ", file=\"file", i, ".rda\")",sep=""))

expression(save(file2, file="file2.rda"))
attr(,"srcfile")
<text>

> paste("save(file", i, ", file="file", i, ".rda")",sep="")
Error: unexpected symbol in "paste("save(file", i, ", file="file"

Hope it's a bit more clear now.
Cheers
Joris
-- 
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



More information about the R-help mailing list