Thank you guys, that works.
I thought the string would first be converted into a formula object in
lda().
using "paste" and "names" is much more concise than
"sprintf".
Best,
Feng
----- Original Message -----
From: "talepanda" <talepanda at gmail.com>
To: "Feng Qiu" <hustqiufeng at sohu.com>
Cc: "Gabor Grothendieck" <ggrothendieck at gmail.com>;
<r-help at stat.math.ethz.ch>
Sent: Friday, December 29, 2006 1:49 AM
Subject: Re: [R] How to write string dynamicly?
> Try this:
>
> lda(formula(paste(names(iris)[5],"~.")),iris)
>
> You have to create *formula* object from string and pass it to lda().
>
> On 12/29/06, Feng Qiu <hustqiufeng at sohu.com> wrote:
>> Hi Gabor:
>> Thank you! But it didn't work. Since lda() takes the
variable
>> name as the input parameter. So what I was trying to do is "make
the name
>> dynamically". I used sprintf() to generate a variable name, such
as
>> "V16". But it seems that the function doesn't recognize
the generated
>> name. For example, lda(V16,data=mydata) works, But,
>> lda(sprintf("V%d",k),data=mydata) does not work, where k=16.
So I guess
>> the
>> name generated by sprintf is not the parameter wanted. But I have no
idea
>> about it.
>>
>> Best,
>>
>> Feng
>>
>>
>> ----- Original Message -----
>> From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
>> To: "Feng Qiu" <fqiu at gatech.edu>
>> Cc: <r-help at stat.math.ethz.ch>
>> Sent: Wednesday, December 27, 2006 1:56 PM
>> Subject: Re: [R] How to write string dynamicly?
>>
>>
>> > Try:
>> >
>> > lda(iris[-5], iris[,5])
>> >
>> > On 12/26/06, Feng Qiu <fqiu at gatech.edu> wrote:
>> >> Hi everyone:
>> >> I'm trying to compose a string dynamicly for the
parameter
>> >> input of
>> >> some function. For example:
>> >> In package MASS, function lda() require to input the name of
predictor
>> >> variable. Let's say the 16th column is the predictor
variable. Then we
>> >> call
>> >> the function like this: lda(V16~., data=mydata). I don't
want to
>> >> hard-code
>> >> the call, instead, I would like to use a dynamic expression
for this
>> >> parameter so that I can use my program on different set of
data.
>> >> I guess there,- are some function that can do this, but
I
>> >> didn't find
>> >> it in "Introduction to R" so far, could someone
please tell me this
>> >> kind of
>> >> function? Thank you!
>> >>
>> >> Best,
>> >>
>> >> Feng
>> >>
>> >> ______________________________________________
>> >> R-help at stat.math.ethz.ch 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.
>> >>
>> >
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>>
>
>