【问题标题】:rbind in lapply cannot understand date formatlapply 中的 rbind 无法理解日期格式
【发布时间】:2014-09-16 09:08:25
【问题描述】:

我有以下代码:

    tradingDates <- c(as.Date("1996-12-31", format = "%Y-%m-%d" ), 
                      as.Date("1997-12-31", format = "%Y-%m-%d" ))
    d1 <- data.frame(CUSIP=c("039229109","M33228109"),
             Port.Weights=as.numeric(c("3.571429","4.976429")), 
             Trade.Date = as.Date("1996-12-31", format = "%Y-%m-%d" ), stringsAsFactors = FALSE)
    d2 <- data.frame(CUSIP=c("432764733","324K32586"),
             Port.Weights=as.numeric(c("6.243803","1.469823")), 
             Trade.Date = as.Date("1997-12-31", format = "%Y-%m-%d" ), stringsAsFactors = FALSE)
    myList <- list(d1, d2)

    thePorts <- lapply(seq_along(myList), function(x)
                       rbind(myList[[x]],
                             c("78462F10", 
                               sum(as.numeric((-.01)*myList[[x]]$Port.Weights[1])), 
                               as.Date(as.character(tradingDates[x]), format= "%Y-%m-%d")),
                             c("CASH_USD",
                               sum(as.numeric((.01)*myList[[x]]$Port.Weights[1])),
                               as.Date(as.character(tradingDates[x]), format= "%Y-%m-%d"))))

我得到的错误是:

     Error in charToDate(x) : character string is not in a standard unambiguous format 

我已经尝试了所有我能想到的方法来格式化这个日期,应该注意的是,在 rbind 和 lapply 之外,这条线工作正常:

    x=5
    format(as.Date(as.character(tradeDates[x]), format= "%Y-%m-%d"), "%Y%m%d")

【问题讨论】:

  • 您共享数据的方式使得创建可重现的示例变得非常困难(即,我不能只是复制粘贴并运行您的代码)。但我认为问题在于您正在转换为日期,但随后您正在使用“%Y%m%d”格式化它(并转换为字符),这将是“19961213”,并且当它使用 rbind 时其他日期值,您会收到该错误,因为这是一种无效的格式。如果您也清楚地指明了此代码所需的输出,这将有所帮助。有许多奇怪的事情,例如将 c() 用于混合数据类型,以及不正确的 stringsAsFactors参数。
  • 我已经编辑了代码。您应该能够复制/粘贴并复制我遇到的错误。非常感谢您的宝贵时间。
  • 编辑让这个问题更容易回答。谢谢。

标签: r date format lapply rbind


【解决方案1】:

问题是您将c() 与混合数据一起使用。类型。 c() 仅用于组合相同类型的元素(除非被包重载),因此它将强制所有内容为相同的 data.type。如果你跑

lapply(seq_along(myList), function(x)
    c("78462F10", 
    sum(as.numeric((-.01)*myList[[x]]$Port.Weights[1])), 
    as.Date(as.character(tradingDates[x]), format= "%Y-%m-%d")))

# [[1]]
# [1] "78462F10"    "-0.03571429" "9861"       
# 
# [[2]]
# [1] "78462F10"    "-0.06243803" "10226"  

您会看到所有内容都转换为字符串。这包括第一次转换为数字的日期,这里用自 1970 年 1 月 1 日以来的天数表示。虽然简单的向量只能保存一种类型的数据,但 list() 能够保存不同类型的数据。所以把你的代码改成

thePorts <- lapply(seq_along(myList), function(x)
    rbind(myList[[x]],
        list("78462F10", 
            sum(as.numeric((-.01)*myList[[x]]$Port.Weights[1])), 
            as.Date(as.character(tradingDates[x]), format= "%Y-%m-%d")),
        list("CASH_USD",
            sum(as.numeric((.01)*myList[[x]]$Port.Weights[1])),
            as.Date(as.character(tradingDates[x]), format= "%Y-%m-%d"))
    )
)

返回

[[1]]
      CUSIP Port.Weights Trade.Date
1 039229109   3.57142900 1996-12-31
2 M33228109   4.97642900 1996-12-31
3  78462F10  -0.03571429 1996-12-31
4  CASH_USD   0.03571429 1996-12-31

[[2]]
      CUSIP Port.Weights Trade.Date
1 432764733   6.24380300 1997-12-31
2 324K32586   1.46982300 1997-12-31
3  78462F10  -0.06243803 1997-12-31
4  CASH_USD   0.06243803 1997-12-31

根据需要(我假设)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-20
    • 2013-07-09
    • 2018-12-04
    • 1970-01-01
    • 2017-10-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多