Re: [R] Passing function arguments to dataset names

From: Janko Thyson <janko.thyson.rstuff_at_googlemail.com>
Date: Tue, 24 May 2011 00:35:03 +0200

Hi Mateo,
not sure if I totally get what you're after, but maybe this helps:

SharpeRatio.annualized <- function(roc){

     print("I'm computing the Sharpe Ratio")
     return(9999)

}

MyF <- function(Tic, price){

     print("Option 1")
     expr <- expression(Ratio.Tic <- SharpeRatio.annualized(roc))
     print(expr)
     eval(expr)
     print(Ratio.Tic)
     rm(Ratio.Tic)

     print("Option 2")
     expr <- paste("Ratio.", quote(Tic), " <- 
SharpeRatio.annualized(roc)", sep="")
     print(expr)
     eval(parse(text=expr))
     print(Ratio.Tic)
     rm(Ratio.Tic)

     print("Option 3")
     frmls <- formals("MyF")
     expr <- sapply(names(frmls), function(x){
         expr <- substitute(NAME <- SharpeRatio.annualized(roc),
             list(NAME=as.name(paste("Ratio", x, sep="."))))
         return(expr)
     })
     print(expr)
     sapply(expr, eval, envir=environment())
     print("Ratio.Tic")
     print(Ratio.Tic)
     print("Ratio.price")
     print(Ratio.price)

}
MyF()

Have fun with R!

Regards,
Janko

On 23.05.2011 23:31, MatAra wrote:
> Hello,
>
> I am stuck in a relatively simple procedure and was wondering if anybody
> knows the answer. I am a relatively new R user.
>
> How do I use an argument of a custom function in the name of a dataset in R?
> For example, I have the function:
>
> MyF<- function(Tic, price){
> xxxxx
> xxxxx
> xxxxx
> Ratio.Tic<- SharpeRatio.annualized(roc)
> }
>
> I would like to have a dataset that's labelled Ratio.MSFT, Ratio.XOM,
> Ratio.IBM...etc. The objective is to append multiple Ratio.Tic datasets that
> contains all the ratios in one single data.
> Thanks in for your time!
> Mateo
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Passing-function-arguments-to-dataset-names-tp3545567p3545567.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

        [[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. Received on Mon 23 May 2011 - 22:37:10 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 24 May 2011 - 04:20:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive