On 14/02/2012 2:23 PM, Nordlund, Dan (DSHS/RDA) wrote:
>  -----Original Message-----
>  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
>  project.org] On Behalf Of Muhammad Rahiz
>  Sent: Tuesday, February 14, 2012 11:03 AM
>  To: x.r-help
>  Subject: [R] execute array of functions
>
>  Hi all,
>
>  I'm trying to get the min and max of a sequence of number using a loop
>  like the folllowing. Can anyone point me to why it doesn't work.
>
>  Thanks.
>
>  type      <- c("min","max")
>  n         <- 1:10
>  for (a in 1:2)      {
>  print(type[a](n)) }
>
>
>  --
>  Muhammad
>

I am not sure why you are trying to use this approach in a loop, but you need 
to use the get() function if you want to do this.

type<- c("min","max")
n<- 1:10
for (a in 1:2)    {
   print(get(type[a])(n))
}

But, why not use min and max on n directly?

Or put min and max into the type vector, i.e.

type <- list(min, max)
n <- 1:10
for (a in 1:2) print(type[[a]](n))

Note that I need the double brackets [[a]], because type is a list. (You can use type <- c(min, max) with the same result, but
I find using list() explicitly is easier to understand.)

Duncan Murdoch

______________________________________________
R-help@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.

Reply via email to