我有以下代码:
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")