Hi everyone check this out [R-1.7.0]: R> f1 <- function(x){x^2} R> f1 -> f2 R> f2(4) [1] 16 R> R> function(x){x^2} -> f3 function(x){x^2} -> f3 R> f3(4) Error: couldn't find function "f3" Why does right assignment "->" work in the first but not the second case? Can anyone else reproduce this? -- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz(nospam) tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
You need an extra level of braces to resolve the ambiguity:> function(x) {x^2} -> f3function(x) {x^2} -> f3> .Last.valuefunction(x) {x^2} -> f3 You need to do it as follows:> {function(x) {x^2}} -> f3 > f3function(x) {x^2} Of course the inner braces around the x^2 are not necessary, now. Bill Venables. -----Original Message----- From: Robin Hankin [mailto:r.hankin at auckland.ac.nz] Sent: Monday, June 23, 2003 1:32 PM To: r-help at stat.math.ethz.ch Subject: [R] right assignment ("->") and functions Hi everyone check this out [R-1.7.0]: R> f1 <- function(x){x^2} R> f1 -> f2 R> f2(4) [1] 16 R> R> function(x){x^2} -> f3 function(x){x^2} -> f3 R> f3(4) Error: couldn't find function "f3" Why does right assignment "->" work in the first but not the second case? Can anyone else reproduce this? -- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz(nospam) tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Mon, 23 Jun 2003, Robin Hankin wrote:> Hi everyone > > check this out [R-1.7.0]: > > R> f1 <- function(x){x^2} > R> f1 -> f2 > R> f2(4) > [1] 16 > R> > R> function(x){x^2} -> f3 > function(x){x^2} -> f3 > R> f3(4) > Error: couldn't find function "f3" > > Why does right assignment "->" work in the first but not the second > case? Can anyone else reproduce this?Because the second is equivalent to function(x) f3 <- {x^2} an anonymous function. Using a right assignment affects the direction of parsing, but only after it is encountered and R is parsed left to right. -- 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
On Mon, 23 Jun 2003, Robin Hankin wrote:> Hi everyone > > check this out [R-1.7.0]: > > R> f1 <- function(x){x^2} > R> f1 -> f2 > R> f2(4) > [1] 16 > R> > R> function(x){x^2} -> f3 > function(x){x^2} -> f3 > R> f3(4) > Error: couldn't find function "f3" > > Why does right assignment "->" work in the first but not the second > case? Can anyone else reproduce this? >It does work. It just doesn't do what you expect. Suppose you typed {x^2} -> f3 This would assign x^2 to f3. So function(x) {x^2} -> f3 is an anonymous function of one argument, which assigns the square of that argument to the local variable f3. To get what you wanted you would need {function(x) x^2}-> f3 -thomas