[R] Pass character vector to function argument

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue Nov 16 18:15:38 CET 2010


Hi,

It would have been nice to see what you've tried.

Here is what I got (maybe not the easiest, but looks like it works)
a1 <- 1
a2 <- 2
obs <- objects(pattern=glob2rx("a?"))
sum(unlist(mget(obs, envir=.GlobalEnv)))
[1] 3

It also works with vectors:
a1 <- 1:10
a2 <- 11:20
sum(unlist(mget(obs, envir=.GlobalEnv)))
[1] 210

Is it what you're looking for?

HTH,
Ivan



Le 11/16/2010 16:34, Kevin Ummel a écrit :
> A bit embarrassed to post this seemingly trivial question, but I can't find anything in the archive that's quite relevant:
>
> a1=1
> a2=2
>
> obs=objects(pattern=glob2rx("a?"))
>
> I want to utilize 'obs' as a function argument to produce something like:
>
> sum(a1,a2)
>
> Obviously, sum(obs) doesn't work, but I've tried variations of 'eval', 'parse', 'mget', and 'noquote' without success. What am I missing?
>
> Thanks,
> Kevin
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php



More information about the R-help mailing list