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]]
______________________________________________ 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.