[R] nameless functions in R
Thomas Lumley
tlumley at u.washington.edu
Wed Dec 3 19:39:16 CET 2003
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
More information about the R-help
mailing list