[R] Converting character to function argument
Uwe Ligges
ligges at statistik.uni-dortmund.de
Fri Sep 12 19:47:06 CEST 2003
Henrik Bengtsson wrote:
> eval() and parse() are you friends here. Possibly also substitute().
> Example:
>
> eval(parse(text="expand.grid(c(1,0),c(1,0))"))
>
> However, I would like to reply with a question to you. Why do you end up
> with a string that you need to convert in the first place?
That's the question, indeed!
> Maybe you are
> more interested in a solution like the following one:
>
> N <- 2
> l <- lapply(1:N, FUN=function(x) c(0,1))
> expand.grid(l)
>
> Henrik Bengtsson
> Lund University
Or, if you are really going to work completely on characters (hence you
have to be more specific!):
do.call("expand.grid", rep(list(eval(parse(text = "c(1, 0)"))), 2))
Uwe Ligges
>
>>-----Original Message-----
>>From: r-help-bounces at stat.math.ethz.ch
>>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
>>Alvaro.Antonio.Novo at bportugal.pt
>>Sent: den 12 september 2003 18:55
>>To: r-help at stat.math.ethz.ch
>>Subject: [R] Converting character to function argument
>>
>>
>>How can one transform a character string into an argument of
>>a function (which is not or I don't want it to be a character string)?
>>
>>Example:
>>
>>
>>>expand.grid(c(1,0),c(1,0)) ## OK
>>
>> Var1 Var2
>>1 1 1
>>2 0 1
>>3 1 0
>>4 0 0
>>
>>
>>
>>>paste(rep("c(0,1)",2),collapse=',') ## to be used below
>>
>>[1] "c(0,1),c(0,1)"
>>
>>>## string is the input I want, but it needs to be coerced to the
>>>correct
>>
>>format
>>
>>
>>>expand.grid(paste(rep("c(0,1)",2),collapse=',')) ## does not get me
>>>there
>>
>> Var1
>>1 c(0,1),c(0,1)
>>
More information about the R-help
mailing list