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
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)) }
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120214/35712578/attachment.pl>
-----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)) }
> 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
-- 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
______________________________________________ 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
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Nordlund, Dan (DSHS/RDA) Sent: Tuesday, February 14, 2012 11:24 AM To: r-help at r-project.org Subject: Re: [R] execute array of functions
-----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?
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
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))) }
This is somewhat less ugly:
funs <- c("min","max")
for (a in funs) {
print(do.call(a, list(n))) }
[1] 1
[1] 10
Does that help?
-s
On Tue, Feb 14, 2012 at 14:02, 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<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]]
______________________________________________ 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
-----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?
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.
{y <-function(x) sin(x)+10} (pi/2) ## pi is a known named constant in R
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
x
at 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
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