次のコードがあります。
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")