Hi, I have an apply statement that looks like:> check.cols <- function(v1, v2) {+ return( identical(v1,v2) ); + }> x[,1] [,2] [,3] [1,] 1 3 3 [2,] 4 5 4 [3,] 2 7 6> apply(x, c(2), check.cols, v2=c(7,8,9))[1] FALSE FALSE FALSE Is it possible to make the function check.cols() inline to the apply statement. Some thing like this: apply(x, c(2), funtion(v1,v2){ identical(v1,v2) }, v2=c(1,4,2)) The above gives me a syntax error. I also tried: apply(x, c(2), fun <- funtion(v1,v2){ return(identical(v1,v2)) }, v2=c(1,4,2)) and I still get a syntax error. Is this type of syntax allowed in R? Thanks, ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- He who is in love with himself has at least this advantage -- he won't encounter many rivals. -- Georg Lichtenberg, "Aphorisms"
On Wed, 3 Dec 2003, Rajarshi Guha wrote:> Hi, > I have an apply statement that looks like: > > > check.cols <- function(v1, v2) { > + return( identical(v1,v2) ); > + } > > x > [,1] [,2] [,3] > [1,] 1 3 3 > [2,] 4 5 4 > [3,] 2 7 6 > > apply(x, c(2), check.cols, v2=c(7,8,9)) > [1] FALSE FALSE FALSE > > Is it possible to make the function check.cols() inline to the apply > statement. Some thing like this: > > apply(x, c(2), funtion(v1,v2){ identical(v1,v2) }, v2=c(1,4,2)) > > The above gives me a syntax error. I also tried: > > apply(x, c(2), fun <- funtion(v1,v2){ return(identical(v1,v2)) }, > v2=c(1,4,2)) > > and I still get a syntax error. > > Is this type of syntax allowed in R? >Yes, anonymous functions are allowed. Anonymous funtions aren't -- you appear to have a typographical problem. -thomas
Rajarshi Guha <rxg218 at psu.edu> writes:> apply(x, c(2), funtion(v1,v2){ identical(v1,v2) }, v2=c(1,4,2)) > > The above gives me a syntax error. I also tried:No wonder! Try with `function' instead of `funtion'. -- Bj?rn-Helge Mevik