[R] 'programatically' list or call objects for use in a function?
Esteemed R users and developers, How does one 'programatically' list or call objects for use in a function? For example, i thought i could do something better than this: save(A.cwb, B.cwb, C.cwb, D.cwb, E.cwb, F.cwb, file=afile.RData) with something like these- prfxs - c(A, B, C, D, E, F) #** save(as.name(paste(prfxs, cwb, sep=.)), file=afile.RData) or this- do.call(save, paste(prfxs, cwb, sep=.), file=afile.RData) Both failed. #** And while i've got your attention- is there a 'letter equivalent' to seq() which would have worked nicely for prfxs? ie., letter.seq(A:F) Thoughts and suggestions sincerely appreciated, Karl -- Karl Brand Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam T +31 (0)10 704 3457 |F +31 (0)10 704 4743 |M +31 (0)642 777 268 __ R-help@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.
Re: [R] 'programatically' list or call objects for use in a function?
On 11/09/2010 8:00 AM, Karl Brand wrote: Esteemed R users and developers, How does one 'programatically' list or call objects for use in a function? It depends on the function. For example, i thought i could do something better than this: save(A.cwb, B.cwb, C.cwb, D.cwb, E.cwb, F.cwb, file=afile.RData) with something like these- prfxs - c(A, B, C, D, E, F) #** save(as.name(paste(prfxs, cwb, sep=.)), file=afile.RData) For save, use list=paste(prfxs, cwb, sep=.). or this- do.call(save, paste(prfxs, cwb, sep=.), file=afile.RData) do.call wants a *list* of arguments, not a single vector of character strings. You could construct a list of the names of the objects you want to save, but that's a pain. Just use the list= argument (which doesn't want a list argument :-). Both failed. #** And while i've got your attention- is there a 'letter equivalent' to seq() which would have worked nicely for prfxs? ie., letter.seq(A:F) Not that I know of, but LETTERS[1:6] will give you the sequence, and it's easy to write a function that you'd call as letter.seq(A, F). Having your function work as in your example would be messy. Duncan Murdoch Thoughts and suggestions sincerely appreciated, Karl __ R-help@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.
Re: [R] 'programatically' list or call objects for use in a function?
Cheers Duncan, for your FAST answers and patience. (fast patience?!) I was close. But closer reading of ?save would also have worked i see now :$. LETTERS[] = :) thanks again, Karl On 9/11/2010 2:10 PM, Duncan Murdoch wrote: On 11/09/2010 8:00 AM, Karl Brand wrote: Esteemed R users and developers, How does one 'programatically' list or call objects for use in a function? It depends on the function. For example, i thought i could do something better than this: save(A.cwb, B.cwb, C.cwb, D.cwb, E.cwb, F.cwb, file=afile.RData) with something like these- prfxs - c(A, B, C, D, E, F) #** save(as.name(paste(prfxs, cwb, sep=.)), file=afile.RData) For save, use list=paste(prfxs, cwb, sep=.). or this- do.call(save, paste(prfxs, cwb, sep=.), file=afile.RData) do.call wants a *list* of arguments, not a single vector of character strings. You could construct a list of the names of the objects you want to save, but that's a pain. Just use the list= argument (which doesn't want a list argument :-). Both failed. #** And while i've got your attention- is there a 'letter equivalent' to seq() which would have worked nicely for prfxs? ie., letter.seq(A:F) Not that I know of, but LETTERS[1:6] will give you the sequence, and it's easy to write a function that you'd call as letter.seq(A, F). Having your function work as in your example would be messy. Duncan Murdoch Thoughts and suggestions sincerely appreciated, Karl -- Karl Brand Department of Genetics Erasmus MC Dr Molewaterplein 50 3015 GE Rotterdam T +31 (0)10 704 3457 |F +31 (0)10 704 4743 |M +31 (0)642 777 268 __ R-help@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.