4

我有一个列表对象如下:

structure(list(bga_u = structure(list(buy_target_price = numeric(0), 
    sell_target_price = numeric(0), units_holding = 0, tx_record = structure(list(
        `2010-12-01` = structure(list(buy_date = structure(14944, class = "Date"), 
            offer_price = 40.58, buy_unit = 3.15931809278285, 
            status = "hold"), .Names = c("buy_date", "offer_price", 
        "buy_unit", "status")), `2011-01-03` = structure(list(
            buy_date = structure(14977, class = "Date"), offer_price = 42.2, 
            buy_unit = 3.03803621339166, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status")), `2011-02-01` = structure(list(
            buy_date = structure(15006, class = "Date"), offer_price = 42.59, 
            buy_unit = 3.01021667539629, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status")), `2011-03-01` = structure(list(
            buy_date = structure(15034, class = "Date"), offer_price = 43.37, 
            buy_unit = 2.95607858439309, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status")), `2011-04-01` = structure(list(
            buy_date = structure(15065, class = "Date"), offer_price = 43.35, 
            buy_unit = 2.95744240380919, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status")), `2011-05-03` = structure(list(
            buy_date = structure(15097, class = "Date"), offer_price = 44.53, 
            buy_unit = 2.87907316876551, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status"))), .Names = c("2010-12-01", 
    "2011-01-03", "2011-02-01", "2011-03-01", "2011-04-01", "2011-05-03"
    ))), .Names = c("buy_target_price", "sell_target_price", 
"units_holding", "tx_record")), bwg_u = structure(list(buy_target_price = numeric(0), 
    sell_target_price = numeric(0), units_holding = 0, tx_record = structure(list(
        `2010-11-02` = structure(list(buy_date = structure(14915, class = "Date"), 
            offer_price = 63.15, buy_unit = 2.03016830095215, 
            status = "hold"), .Names = c("buy_date", "offer_price", 
        "buy_unit", "status")), `2010-12-01` = structure(list(
            buy_date = structure(14944, class = "Date"), offer_price = 64.19, 
            buy_unit = 1.99727571592348, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status")), `2011-09-01` = structure(list(
            buy_date = structure(15218, class = "Date"), offer_price = 69.29, 
            buy_unit = 1.85026884406304, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status")), `2011-10-03` = structure(list(
            buy_date = structure(15250, class = "Date"), offer_price = 58.05, 
            buy_unit = 2.20852934031229, status = "hold"), .Names = c("buy_date", 
        "offer_price", "buy_unit", "status"))), .Names = c("2010-11-02", 
    "2010-12-01", "2011-09-01", "2011-10-03"))), .Names = c("buy_target_price", 
"sell_target_price", "units_holding", "tx_record"))), .Names = c("bga_u", 
"bwg_u"))

这是一个由 3 个级别组成的“列表中的列表”对象:

  • 级别 1:所有基金的名称(这里有 2 个基金:bga_u 和bwg_u
  • 级别 2:目标买入价;目标售价;和持有单位
  • 第 3 级:特定交易的详细信息(例如购买的单位)

如果该特定交易的状态仍为“持有”,则存储在级别 2 中的“持有单位”是通过将级别 3 中每笔交易的“买入单位”相加来计算的。

如何计算每个基金的“单位持有量”并放入第 2 级?

(我使用 R 2-3 年,但我对这个list对象很陌生,我可能在概念上是错误的list。)

4

3 回答 3

3

这是一种方法(假设对象是 names dat):

lapply(dat, function(x) 
  "[[<-"(x, "units_holding", 
         sum(sapply(x[["tx_record"]], function(y) 
           ifelse(y[["status"]] == "hold", y[["buy_unit"]], 0)))))

在此命令中,lapplysapply分别用于迭代列表的第一级和第二级。

笔记。这将创建一个新对象,您可能希望将其分配给新变量(或覆盖旧变量)。

于 2012-12-27T10:23:23.277 回答
2

假设整个列表被命名为dl,将函数应用于列表的每个第一级(基金)。首先,制作交易记录的数据框,然后对单位求和并放入units_holding

for(i in 1:length(dl)){
  df<-do.call(rbind,lapply(dl[[i]]$tx_record,data.frame))
  dl[[i]]$units_holding<-sum(df$buy_unit[df$status=="hold"])
}
于 2012-12-27T10:47:55.850 回答
0

这是一个版本,它试图使在获取购买价格时执行的操作尽可能不透明。status如果不是,我们将返回 0 hold,因此将其相加不会影响最终总数。

通常,如果您在 R 中遍历列表并统一处理每个组件,则可以使用lapplyorsapply函数。但是,如果您有特别大量的事务,这可能会很慢。

## assume your data is available in a variable 'dat'
get_buy_price <- function(x) {
  if( x[["status"]] == "hold" ) {
    return( x[["buy_unit"]] )
  } else {
    return( 0 )
  }
}

buy_prices <- sapply( dat, function( fund ) {
  sapply( fund["tx_record"], function( date ) {
    sapply( date, get_buy_price )
  })
})

totals <- lapply( buy_prices, sum )

for( fund_ind in seq_along(dat) ) {
  dat[[fund_ind]]$units_holding <- totals[[fund_ind]]
}
于 2012-12-27T10:31:21.350 回答