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
> -----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
That won't work because R has special rules for evaluating things in the
function position. Examples:
*OK*
min(1:2)
"min"(1:2)
f<-min; f(1:2)
do.call(min,list(1:2))
do.call("min",list(1:2)) # do.call converts string->function
*Not OK*
("min")(1:2) # string in function position is not
converted
f<-"min"; f(1:2) # ditto
f<- c("min","max"); f[1](1:2) # ditto
What you need to do is make 'f' a list of *function values, *not a
vector
of strings:
f<- c(min,max)
and then select the element of f with [[ ]] (select one element), not [ ]
(select sublist):
f[[1]](1:2)
Thus your example becomes
type <- c(min,max)
n <- 1:10
for (a in 1:2) {
print(type[[a]](n)) }
Another (uglier) approach is with do.call:
type <- c("min","max")
n <- 1:10
for (a in 1:2) {
print(do.call(type[a],list(n))) }
Does that help?
-s
On Tue, Feb 14, 2012 at 14:02, Muhammad Rahiz
<muhammad.rahiz@ouce.ox.ac.uk>wrote:
> 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
>
> ______________________________**________________
> R-help@r-project.org mailing list
>
https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
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.> {y <-function(x) sin(x)+10} (pi/2) ## pi is a known named constant in R11 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>xat the command line would print the values (by default) typing>"x"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:> 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 > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm