An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061208/89cb6914/attachment-0005.pl
Remove " from a string
6 messages · Katharina Vedovelli, jim holtman, Scionforbai +3 more
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061208/f33d91a7/attachment-0005.pl
If I understand what you need,
Number=2
x <- paste("NameOfFunction",as.character(Number),sep="")
x
[1] "NameOfFunction2" And you can use do.call(x, ...) to "get" your function. Hope it helps, Scionforbai
On Fri, 2006-12-08 at 14:57 +0100, Katharina Vedovelli wrote:
Hi all!
I have lots of functions called in the following pattern
'NameOfFunctionNumber' where the name always stays the same and the number
varies from 1 to 98.
Another function which I run in advance returns the number of the function
which has to be called next.
Now I want to combine 'NameOfFunction' with the 'Number' returned so that i
can call the desired function.
I do this by:
x<-c("NameOfFunction",Number)
z<-paste(x,collapse="")
z
which returns
"NameOfFunctionNumber"
My Problem is that R doesn't recognise this as the name of my function
because of the " at the beginning and the end.
Is there a way of getting rid of those? Or does anybody know another way of
solving this problem?
Thanks a lot for your help!
Cheers,
Katharina
It is not entirely clear what your ultimate goal is, thus there may be a (much) better approach than calling functions in this manner. What do the functions actually do and does the output vary based upon some attribute (ie. the class) of the argument such that using R's typical function dispatch method would be more suitable. However, to address the specific question, at least two options: NameOfFunction21 <- function(x) x^2
eval(call(paste("NameOfFunction", 21, sep = ""), 21))
[1] 441
do.call(paste("NameOfFunction", 21, sep = ""), list(21))
[1] 441 In both cases, the result is to evaluate the function call, with 21 as the argument. See ?call, ?eval and ?do.call for more information. HTH, Marc Schwartz
try this: f1 <- function(x) x + 1 f2 <- function(x) x + 2 f3 <- function(x) x + 3 ########### FunNam <- "f1" eval(call(FunNam, x = 1:5)) FunNam <- "f2" eval(call(FunNam, x = 1:5)) FunNam <- "f3" eval(call(FunNam, x = 1:5)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Katharina Vedovelli" <katharina.vedovelli at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Friday, December 08, 2006 2:57 PM Subject: [R] Remove " from a string
Hi all!
I have lots of functions called in the following pattern
'NameOfFunctionNumber' where the name always stays the same and the
number
varies from 1 to 98.
Another function which I run in advance returns the number of the
function
which has to be called next.
Now I want to combine 'NameOfFunction' with the 'Number' returned so
that i
can call the desired function.
I do this by:
x<-c("NameOfFunction",Number)
z<-paste(x,collapse="")
z
which returns
"NameOfFunctionNumber"
My Problem is that R doesn't recognise this as the name of my
function
because of the " at the beginning and the end.
Is there a way of getting rid of those? Or does anybody know another
way of
solving this problem?
Thanks a lot for your help!
Cheers,
Katharina
[[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch 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.
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
I second Marc's comments below, but for amusement, another alternative to the (undesirable) eval(call()) construction is:
foo<-function(x)x^2
get("foo")(1:5)
[1] 1 4 9 16 25 I believe this is equally undesirable, however, and as Marc said, making your function a function of two arguments or something similar would be the better approach. Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Marc Schwartz Sent: Friday, December 08, 2006 6:14 AM To: Katharina Vedovelli Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Remove " from a string
On Fri, 2006-12-08 at 14:57 +0100, Katharina Vedovelli wrote:
Hi all! I have lots of functions called in the following pattern 'NameOfFunctionNumber' where the name always stays the same and the number varies from 1 to 98. Another function which I run in advance returns the number of the function which has to be called next. Now I want to combine 'NameOfFunction' with the 'Number' returned so that
i
can call the desired function.
I do this by:
x<-c("NameOfFunction",Number)
z<-paste(x,collapse="")
z
which returns
"NameOfFunctionNumber"
My Problem is that R doesn't recognise this as the name of my function
because of the " at the beginning and the end.
Is there a way of getting rid of those? Or does anybody know another way
of
solving this problem? Thanks a lot for your help! Cheers, Katharina
It is not entirely clear what your ultimate goal is, thus there may be a (much) better approach than calling functions in this manner. What do the functions actually do and does the output vary based upon some attribute (ie. the class) of the argument such that using R's typical function dispatch method would be more suitable. However, to address the specific question, at least two options: NameOfFunction21 <- function(x) x^2
eval(call(paste("NameOfFunction", 21, sep = ""), 21))
[1] 441
do.call(paste("NameOfFunction", 21, sep = ""), list(21))
[1] 441 In both cases, the result is to evaluate the function call, with 21 as the argument. See ?call, ?eval and ?do.call for more information. HTH, Marc Schwartz ______________________________________________ R-help at stat.math.ethz.ch 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.