[R] using the "apply" method for functions with multiple inputs

David Winsemius dwinsemius at comcast.net
Tue Nov 23 03:43:25 CET 2010


On Nov 22, 2010, at 8:38 PM, joeponzio wrote:

>
> hello r users,
>
> i'm trying to use the apply method on a function with several  
> inputs, but
> cannot figure out how to send multiple arguments to the function (not
> multiple runs of the same function, but one run of the function  
> including
> two variables - each used within the function).
>
> a <- c(1:10,999,999,999)
> b <- c(11:20,999,999,999)
>
> tfun <- function(x,y){
>  if( (x = 1 & y !=999) || (x > 1 & x < 999 & y == 999) )

There is a problem with that first logical test, assigns 1 to x rather  
than testing.

>    x1 <- 1
>  else
>    x1 <-0
> }
>

 > mapply("tfun", a, b)
  [1] 1 1 1 1 1 1 1 1 1 1 0 0 0

(but see above if that were not what you expected.)

> #this doesn't work - gives an error " 'y' is missing
> tfilt <- sapply(data.frame(a,b), tfun)
>
> thanks,
> joe
>
> -- 
> View this message in context: http://r.789695.n4.nabble.com/using-the-apply-method-for-functions-with-multiple-inputs-tp3054719p3054719.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list