Skip to content

execute array of functions

8 messages · Muhammad Rahiz, Nordlund, Dan (DSHS/RDA), Stavros Macrakis +3 more

#
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)) }
#
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?


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
On Feb 14, 2012, at 2:23 PM, Nordlund, Dan (DSHS/RDA) wrote:

            
> n=1:10
 > for (a in c(min,max)) 	  {
+ print(a(n)) }
[1] 1
[1] 10

 > funs <- c(min,max)
 > for (a in funs) 	  {
+ print(a(n)) }
[1] 1
[1] 10
David Winsemius, MD
West Hartford, CT
#
I probably should have pointed out that the reason your code doesn't work is that type is a vector of character strings with function names, not the actual functions.  Another approach would be to store the actual functions, rather than the names in type.  So something like 

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


But maybe someone more expert can tell you the 'best' or more R-ish way of accomplishing what you want. 

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
#
On Feb 14, 2012, at 2:32 PM, Stavros Macrakis wrote:

            
This is somewhat less ugly:

  funs <- c("min","max")
  for (a in funs) 	  {
    print(do.call(a, list(n))) }

[1] 1
[1] 10
David Winsemius, MD
West Hartford, CT
#
On 14/02/2012 2:23 PM, Nordlund, Dan (DSHS/RDA) wrote:
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
#
Not sure the replies you received make it clear, but the key point is
that functions are first class objects in R like any other objects
(vectors, lists, data frames) and can be assigned, collected into
structures, indexed, etc. in the usual way. Further, if the value of
any R expression is a function object, it can be called in the usual
way on an argument list by:

Some R_Expression(comma separated argument list)

e.g.
11

Your initial confusion below actually has nothing to do with this: you
have confused unquoted names of objects in R with quoted character
strings. So, e.g.

x <- 1:10
assigns a sequence of numeric values to the name x, which essentially
points to where those values are stored in memory.

typing
at the command line would print the values (by default)

typing
prints the character string "x"

-- Bert

On Tue, Feb 14, 2012 at 11:02 AM, Muhammad Rahiz
<muhammad.rahiz at ouce.ox.ac.uk> wrote: