Message-ID: <F7E6D18CC2877149AB5296CE54EA276603B8E3@WAXMXOLYMB001.WAX.wa.lcl>
Date: 2012-02-14T19:23:33Z
From: Nordlund, Dan (DSHS/RDA)
Subject: execute array of functions
In-Reply-To: <alpine.LRH.2.02.1202141858380.3167@finland.ouce.ox.ac.uk>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at 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?
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