[R] Q: selecting a name when it is known as a string

Tony Plate tplate at acm.org
Wed Sep 5 21:22:13 CEST 2007


For the column names of the result of expand.grid(), I would just assign 
them the values I wanted, like this:

 > x <- expand.grid(tmp=1:3,y=1:2)
 > x
   tmp y
1   1 1
2   2 1
3   3 1
4   1 2
5   2 2
6   3 2
 > colnames(x)[1] <- "whatever"
 > x
   whatever y
1        1 1
2        2 1
3        3 1
4        1 2
5        2 2
6        3 2
 >

-- Tony Plate

D. R. Evans wrote:
> D. R. Evans said the following at 09/04/2007 04:14 PM :
>> I am 100% certain that there is an easy way to do this, but after
> 
> I have reconsidered this and now believe it to be essentially impossible
> (or at the very least remarkably difficult) although I don't understand why
> it is so :-(
> 
> At least, I spent another two hours trying variations on the suggestions I
> received, but still nothing worked properly.
> 
> It sure seems like it _ought_ to be easy, because of the following argument:
> 
> If I type an expression such as "A <- <something>" then R is perfectly
> capable of parsing the <something> and executing it and assigning the
> result to A. So it seems to follow that it ought to be able to parse a
> string that contains exactly the same sequence of characters (after all,
> why should the R parsing engine care whether the input string comes from
> the terminal or from a variable?) and therefore it should be possible to
> assign "<something>" to a variable and then have R parse that variable
> precisely as if it had been typed.
> 
> That was my logic as to why this ought to be easy, anyway. (And there was
> the subsidiary argument that this is easy in the other languages I use, but
> R is sufficiently different that I'm not certain that that argument carries
> much force.)
> 
> It does seem that there are several ways to make the
> 
>   lo <- loess(percent ~ ncms * ds, d, control=loess.control(trace.hat =
>> 'approximate'))
> 
> command work OK if the right hand side is in a character variable, but I
> haven't been able to find a way to make
> 
>   grid <- data.frame(expand.grid(ds=MINVAL:MAXVAL, ncms=MINCMS:MAXCMS))
> 
> work.
> 
> I always end up with a parse error or a complaint that "'newdata' does not
> contain the variables needed" when I perform the next task:
> 
>   plo <- predict(lo, grid).
> 
> So I guess I have to stick with half a dozen compound "if" statements, all
> of which do essentially the same thing :-(
> 
> ______________________________________________
> 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