Dear list, given this formula:> fmla <- formula(y1 ~ spp1 + spp2 + spp3 + spp5) > fmla[[3]]spp1 + spp2 + spp3 + spp5 is this the intended behaviour of as.character:> as.character(fmla[[3]])[1] "+" "spp1 + spp2 + spp3" "spp5" ? Where does the extra "+" come from?> as.character(fmla)[1] "~" "y1" [3] "spp1 + spp2 + spp3 + spp5" Thanks in advance, Gav -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpsonATNOSPAMucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson wrote:> Dear list, > > given this formula: > > >>fmla <- formula(y1 ~ spp1 + spp2 + spp3 + spp5) >>fmla[[3]] > > spp1 + spp2 + spp3 + spp5 > > is this the intended behaviour of as.character: > > >>as.character(fmla[[3]]) > > [1] "+" "spp1 + spp2 + spp3" "spp5" > > ? Where does the extra "+" come from?Which *extra* "+"? This expression is the same as "+"(spp1 + spp2 + spp3, spp5) hence "+" with arguments "spp1 + spp2 + spp3" and "spp5" Same below. Uwe Ligges> >>as.character(fmla) > > [1] "~" "y1" > [3] "spp1 + spp2 + spp3 + spp5" > > Thanks in advance, > > Gav
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():> deparse(fmla[[3]])[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 wrote: > >> Dear list, >> >> given this formula: >> >> >>> fmla <- formula(y1 ~ spp1 + spp2 + spp3 + spp5) >>> fmla[[3]] >> >> spp1 + spp2 + spp3 + spp5 >> >> is this the intended behaviour of as.character: >> >> >>> as.character(fmla[[3]]) >> >> [1] "+" "spp1 + spp2 + spp3" "spp5" >> >> ? Where does the extra "+" come from? > > Which *extra* "+"? > > This expression is the same as > > "+"(spp1 + spp2 + spp3, spp5) > > hence "+" with arguments "spp1 + spp2 + spp3" and "spp5" > > Same below. > > Uwe Ligges > > >> >>> as.character(fmla) >> >> [1] "~" "y1" >> [3] "spp1 + spp2 + spp3 + spp5" >> >> Thanks in advance, >> >> Gav > > ______________________________________________ > 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 >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Gavin Simpson <gavin.simpson at ucl.ac.uk> writes:> Dear list, > > given this formula: > > > fmla <- formula(y1 ~ spp1 + spp2 + spp3 + spp5) > > fmla[[3]] > spp1 + spp2 + spp3 + spp5 > > is this the intended behaviour of as.character: > > > as.character(fmla[[3]]) > [1] "+" "spp1 + spp2 + spp3" "spp5"Yes.> ? Where does the extra "+" come from?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] == "~" -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Tue, 2005-08-16 at 21:44 +0200, Peter Dalgaard wrote:> Gavin Simpson <gavin.simpson at ucl.ac.uk> writes: > > > Dear list, > > > > given this formula: > > > > > fmla <- formula(y1 ~ spp1 + spp2 + spp3 + spp5) > > > fmla[[3]] > > spp1 + spp2 + spp3 + spp5 > > > > is this the intended behaviour of as.character: > > > > > as.character(fmla[[3]]) > > [1] "+" "spp1 + spp2 + spp3" "spp5" > > Yes.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> > ? Where does the extra "+" come from? > > 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] == "~" > >-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpsonATNOSPAMucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%