[R] combining mathematical notation and value substitution

Faheem Mitha faheem at email.unc.edu
Fri Jun 27 01:11:50 CEST 2003



On Sun, 22 Jun 2003, Thomas Lumley wrote:

> On Sun, 22 Jun 2003, Faheem Mitha wrote:
> > If I'm doing this correctly, R does not seem to think it is a call.
> >
> > > is.call("Monotonic Multigamma run (" * n == len * ", " * theta == t1
> > * ").")
> > Error in "Monotonic Multigamma run (" * n :
> >         non-numeric argument to binary operator
>
> R is trying to *evaluate*
>   "Monotonic Multigamma run ("* n==len etc
> which doesn't work.  Remember, is.call(), like any normal function, will
> be passed the *value* of its arguments.

Hmm. I'm trying to distinguish in my mind the value of an expression and
the expression itself. For some reason it reminds me of the following
exchange, from "Through the Looking-Glass".

**********************************************************************

"...The name of the song is called 'Haddocks' Eyes.'"

"Oh, that's the name of the song, is it?" Alice said, trying to feel
interested.

"No, you don't understand," the Knight said, looking a little vexed.
"That's what the name is called. The name really is 'The Aged, Aged Man.'"

"Then I ought to have said 'That's what the song is called'?" Alice
corrected herself.

"No you oughtn't: that's another thing. The song is called 'Ways and
Means' but that's only what it's called, you know!"

"Well, what is the song then?" said Alice, who was by this time completely
bewildered.

"I was coming to that," the Knight said. "The song really is 'A-sitting On
a Gate': and the tune's my own invention."
**********************************************************************

> You could try
>   is.call(quote("Monotonic Multigamma run("*n==len))
> which is TRUE.
>
> > It considers it a valid R expression though.
> >
> > > (mode(expression("Monotonic Multigamma run (" * n == len * ", " * theta
> > == t1 * ").")))
> > [1] "expression"
> >
>
> That's because expression() returns an expression.
>
> >
> > The clearest description I have seen of a call is in S Poetry, where it
> > says
> >
> > "Mode call represents objects that are calls to a function. The first
> > component of a call is the name (mode name) of the function being called.
> > The rest of the call is the arguments given."
> >
> > This certainly is how calls are constructed using call(...), but I'm not
> > sure how it fits in with an expression like the one above. What is the
> > function being called in that case, for example?
>
> Well, we can find out. It must be either * or ==, but it isn't immediately
> obvious which one ends up at the top level
>
> > thing <- quote("Monotonic Multigamma Run ("*n==len* ", " * theta
> ==t1*").")
> > mode(thing)
> [1] "call"
> > length(thing)
> [1] 3
> > thing[[1]]
> ==
> > thing[[2]]
> "Monotonic Multigamma Run (" * n == len * ", " * theta
> > thing[[3]]
> t1 * ")."
> > mode(thing[[2]])
> [1] "call"
> > mode(thing[[3]])
> [1] "call"
> > thing[[2]][[1]]
> ==
> > thing[[3]][[1]]
> *
>
> So it is a call to ==, with two arguments, each itself a call.  The first
> arguemetn is also a call to == and the second is a call to *. And so on in
> a tree structure.

This is very interesting. I had convinced myself that an expression could
not become a call unless created explicitly by call, because it could not
know out of all the possible call structures which one to turn the
expression into. However, it appears this is not the case. So, naturally,
this makes me wonder, what rules are used to make the structure, out of
all the various possibilities. For example, the function in the call could
have corresponded to one of the *'s, and then the rest of the structure
would have been different. And is this rule part of the language
definition?

                                                            Faheem.




More information about the R-help mailing list