Skip to content

as.character and a formula

5 messages · Uwe Ligges, Brian Ripley, Peter Dalgaard +1 more

#
Dear list,

given this formula:
spp1 + spp2 + spp3 + spp5

is this the intended behaviour of as.character:
[1] "+"                  "spp1 + spp2 + spp3" "spp5"

? Where does the extra "+" come from?
[1] "~"                         "y1"                       
[3] "spp1 + spp2 + spp3 + spp5"

Thanks in advance,

Gav
#
Gavin Simpson wrote:

            
Which *extra* "+"?

This expression is the same as

"+"(spp1 + spp2 + spp3, spp5)

hence "+" with arguments "spp1 + spp2 + spp3" and "spp5"

Same below.

Uwe Ligges
#
I guess the problem is that Gavin is unaware of what [[]] does for a call.
It is still a call and so you want to use deparse() and not 
as.character():
[1] "spp1 + spp2 + spp3 + spp5"

Watch out for the line length limit on deparse() if you do this in 
programs.
On Tue, 16 Aug 2005, Uwe Ligges wrote:

            

  
    
#
Gavin Simpson <gavin.simpson at ucl.ac.uk> writes:
Yes.
What extra "+" ? There are three of them in fmla[[3]] and three in
as.character(....).

as.character of an object of mode call is obtained by converting it to
a list and deparsing each term (modulo some details regarding
backquotes). This is somewhat peculiar, but quite a bit of legacy code
is depending on it. Things like testing for as.character(e)[1] == "~"
#
On Tue, 2005-08-16 at 21:44 +0200, Peter Dalgaard wrote:
Thanks Uwe, Brian and Peter for setting me straight. Being unobservant,
forgetful and stupid, all in one day, is some going, even for me.

All the best,

Gav