[R] How to write string dynamicly?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Dec 29 07:44:12 CET 2006


Try this:

idx <- match("Species", names(iris))
lda(iris[-idx], iris[,idx])


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.

That is not correct.  It takes the variable itself, not its name, as input.

> 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,

That is not a valid argument sequence.  See ?lda

> 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.
> >>
> >
>
>



More information about the R-help mailing list