Dear R forum

Please refer to my query captioned Error with function.

I had missed in bracket ")" in the return statement and hence I was getting the 
error. I has struggled for more than 2 hours to find out the problem and only 
then has posted to the forum. I sincerely apologize to all for consuming your 
valuable time.

Thanks for the efforts at your end.

Regards

Katherine





--- On Tue, 23/4/13, Katherine Gobin <katherine_go...@yahoo.com> wrote:

From: Katherine Gobin <katherine_go...@yahoo.com>
Subject: [R] Error with function
To: r-help@r-project.org
Date: Tuesday, 23 April, 2013, 7:06 AM

Dear R forum,

I have a data.frame as given below:

df = data.frame(tran = c("tran1", "tran2", "tran3", "tran4"), tenor = c("2w", 
"1m", "7m", "3m"))  

Also, I define

libor_tenor_labels = as.character(c("o_n", "1w", "2w", 
"1m", "2m", "3m", "4m", "5m", "6m", "7m", "8m", "9m", "10m", "11m", 
"12m"))

# ________________________

> df
   tran tenor
1 tran1    2w
2 tran2    1m
3 tran3    7m
4 tran4    3m

# __________________________________________

# libor_tenor_labels can be anything and need not be 15. Also, df need not be 
consisting of only 4 record. Basically, I can't HARD CODE anything.

In df, first tenor is 2w. So I need to define a previous tenor as "1w" and nest 
tenor as "1m" i.e. I need the output

> df_new
   tran tenor prev_tenor nxt_tenor
1 tran1    2w         1w        1m
2 tran2    1m         2w        2m
3 tran3    7m         6m        8m
4 tran4    3m         2m        4m

# _______________________________________

# I have two special cases also. If the tenor is "o_n" or "12m" i.e. extremes, 
I needed to adjust the rates as given in code.

# My code
# ======


tenor_function = function(tran, tenor)

{

if (tenor == libor_tenor_labels[1])
   
   {
 prev_tenor = libor_tenor_labels[1]
 nxt_tenor = libor_tenor_labels[2]
   }

for (i in 2:(length(libor_tenor_labels)-1))

   {
if (tenor == libor_tenor_labels[i])
         {
         prev_tenor = libor_tenor_labels[i-1]
         nxt_tenor = libor_tenor_labels[i+1]
       }
  }

 if (tenor == libor_tenor_labels[length(libor_tenor_labels)])
     {
     prev_tenor = libor_tenor_labels[(length(libor_tenor_labels)-1)]
     nxt_tenor = libor_tenor_labels[length(libor_tenor_labels)]
     }

   return(data.frame(tran = tran, prev_tenor = prev_tenor, tenor = tenor, 
nxt_tenor = nxt_tenor)

}

(tenor_libors = ddply(.data = df, .variables = "tran", .fun = function(x) 
tenor_function(tran = x$tran, tenor = x$tenor)))

# __________________________________________

# ERROR - I get following error


Error: unexpected '}' in:
"
}"
> 
> (tenor_libors = ddply(.data = df, .variables = "tran", .fun = function(x) 
> tenor_function(tran = x$tran, tenor = x$tenor)))
Error in .fun(piece, ...) : could not find function "tenor_function"

# ______________________________________________

Kindly guide

With warn regards

Katherine
















    [[alternative HTML version deleted]]


-----Inline Attachment Follows-----

______________________________________________
R-help@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@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.

Reply via email to