[Blotter-commits] r948 - in pkg/FinancialInstrument: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 29 15:29:36 CET 2012


Author: gsee
Date: 2012-02-29 15:29:36 +0100 (Wed, 29 Feb 2012)
New Revision: 948

Modified:
   pkg/FinancialInstrument/R/buildHierarchy.R
   pkg/FinancialInstrument/man/buildHierarchy.Rd
Log:
 New buildHierarchy function. Thanks Alexis Petit!

Modified: pkg/FinancialInstrument/R/buildHierarchy.R
===================================================================
--- pkg/FinancialInstrument/R/buildHierarchy.R	2012-02-29 12:05:34 UTC (rev 947)
+++ pkg/FinancialInstrument/R/buildHierarchy.R	2012-02-29 14:29:36 UTC (rev 948)
@@ -15,40 +15,31 @@
 
 #' Construct a hierarchy of instruments useful for aggregation
 #'
-#' All 'currency' instruments must be defined before instruments of other types may be defined.
+#' All 'currency' instruments must be defined before instruments of other types 
+#' may be defined.
 #'
-#' In \dots you may pass any other arbitrary instrument fields that will be used to create 'custom' fields.
+#' In \dots you may pass any other arbitrary instrument fields that will be used 
+#' to create 'custom' fields. (This is not yet implemented)
 #'
-#' @param primary_ids A list of assets to be included in the hierarchy list
-#' @param levels A list of instrument attributes in top-down order
-#' @param ... any other passthru parameters
-#' @author Peter Carl
-#' @return Constructs a data.frame that contains the list of assets in the first column and the category or factor for grouping at each level in the following columns
-# @seealso
+#' @param primary_ids A character vector of \code{instrument} primary_ids to be 
+#' included in the hierarchy list
+#' @param levels A character vector of instrument attributes in top-down order
+#' @param ... not in use
+#' @author Peter Carl, Alexis Petit, Garrett See
+#' @return Constructs a data.frame that contains the list of assets in the first 
+#' column and the category or factor for grouping at each level in the following 
+#' columns
+#' @seealso \code{\link{instrument.table}}
 # TODO add a link to PortfolioAnalytics attribution functions, when they exist
 #' @export
 buildHierarchy <- function(primary_ids, levels, ...) {
-    out = data.frame(NA)
-    primary_ids = make.names(primary_ids)
-    for(primary_id in primary_ids) {
-        tmp_instr = try(getInstrument(primary_id))
-        # TODO finish error checking
-        # for each instrument, create a vector of factors at each level
-        attrs = NA
-        for(level in levels){
-            attr = unname(tmp_instr[eval(level)])
-            if(!all(is.na(attrs)))
-                attrs = cbind(attrs, attr)
-            else
-                attrs = attr
-        }
-        if(!is.na(out[1]))
-            out = rbind(out, attrs)
-        else
-            out = attrs
+    out <- data.frame(primary_ids, stringsAsFactors=FALSE)
+    ilist <- lapply(primary_ids, getInstrument)
+    for (level in levels) {
+        tmp_level <- as.character(lapply(1:length(primary_ids), 
+                                         function(x) ilist[[x]][[level]]))
+        out <- cbind(out, tmp_level, stringsAsFactors=FALSE)
     }
-    out = cbind(primary_ids, out)
-    colnames(out) = c("primary_id", levels)
-    out
-
+    colnames(out) <- c("primary_id", levels)
+    return(out)
 }

Modified: pkg/FinancialInstrument/man/buildHierarchy.Rd
===================================================================
--- pkg/FinancialInstrument/man/buildHierarchy.Rd	2012-02-29 12:05:34 UTC (rev 947)
+++ pkg/FinancialInstrument/man/buildHierarchy.Rd	2012-02-29 14:29:36 UTC (rev 948)
@@ -5,13 +5,14 @@
   buildHierarchy(primary_ids, levels, ...)
 }
 \arguments{
-  \item{primary_ids}{A list of assets to be included in the
+  \item{primary_ids}{A character vector of
+  \code{instrument} primary_ids to be included in the
   hierarchy list}
 
-  \item{levels}{A list of instrument attributes in top-down
-  order}
+  \item{levels}{A character vector of instrument attributes
+  in top-down order}
 
-  \item{...}{any other passthru parameters}
+  \item{...}{not in use}
 }
 \value{
   Constructs a data.frame that contains the list of assets
@@ -24,9 +25,13 @@
 }
 \details{
   In \dots you may pass any other arbitrary instrument
-  fields that will be used to create 'custom' fields.
+  fields that will be used to create 'custom' fields. (This
+  is not yet implemented)
 }
 \author{
-  Peter Carl
+  Peter Carl, Alexis Petit, Garrett See
 }
+\seealso{
+  \code{\link{instrument.table}}
+}
 



More information about the Blotter-commits mailing list