毎日(TRADEDATE)BOOKとCOMMODITYで一意に識別されるエントリがあります。各エントリには、毎日変化する現在価値(PV)があります。前月の最終暦日とのPVの差の列を取得したいと思います。私はループを使ってそれを解決しましたが、誰かがもっとエレガントな(そしてより速い解決策)を提案できるかどうか疑問に思いました:
library(data.table)
bwTab
COMMODITY BOOK TRADEDATE PV Desired Column
1: CASH HS_OPT_GEN 2012-09-30 66669.68 NA
2: CASH HS_OPT_GEN 2012-10-01 76333.83 9664.15
3: CASH HS_OPT_GEN 2012-10-02 76333.83 9664.15
4: CASH HS_OPT_GEN 2012-10-03 76333.83 9664.15
5: CASH HS_OPT_GEN 2012-10-04 76333.83 9664.15
---
3050: OIL HO_OIL_FIN 2012-09-30 21330.55 NA
---
3066: OIL HO_OIL_FIN 2012-10-26 42661.28 21330.73
3067: OIL HO_OIL_FIN 2012-10-27 21330.69 0.14
3068: OIL HO_OIL_FIN 2012-10-28 21330.68 0.13
3069: OIL HO_OIL_FIN 2012-10-29 21330.78 0.23
# Here is my solution
# Define a function for last day of previous month
pme <- function(date) {as.Date(paste("01",month(date),year(date),sep="."),"%d.%m.%Y")-1}
difftopme <- function(a) {
if (nrow(bwTab[COMMODITY==a[,COMMODITY] & BOOK==a[,BOOK] & TRADEDATE==pme(a[,STICHTAG]),])==0) {NA} else {
a[,PV]-bwTab[COMMODITY==a[,COMMODITY] & BOOK==a[,BOOK] & TRADEDATE==pme(a[,TRADEDATE]),PV] }
}
for (i in 1:nrow(bwTab)){a <- difftopme(bwTab[i,]) ; if (i==1){diffPVme <- a} else {diffPVme <- c(a,diffPVme)}}
#########################
dput(bwTab[1000:1010,])
structure(list(COMMODITY = c("ELEC", "ELEC", "ELEC", "ELEC",
"ELEC", "ELEC", "ELEC", "ELEC", "ELEC", "ELEC", "ELEC"), BOOK = c("HS_OUK_MKT",
"HS_OUK_MKT", "HS_OUK_MKT", "HS_OUK_MKT", "HS_OUV_EVO", "HS_OUV_EVO",
"HS_OUV_EVO", "HS_OUV_EVO", "HS_OUV_EVO", "HS_OUV_EVO", "HS_OUV_EVO"
), STICHTAG = structure(c(1353798000, 1353970800, 1354057200,
1354143600, 1348956000, 1349042400, 1349128800, 1349215200, 1349301600,
1349388000, 1349474400), class = c("POSIXct", "POSIXt"), tzone = ""),
BROKERAGE = c(123406.66, 61791.17, 62229.17, 62492.57, 0,
0, 0, 0, 0, 0, 0), DV = c(72873524.86, 38096138.75, 38283589.07,
38236199.05, 23171721.81, 23178889.59, 23187553.93, 23187426.98,
23173154.67, 23149439.13, 23149469.88), REALIZED = c(47002372.1,
23501186.05, 23501186.05, 23501186.05, 22961528, 22961528,
22961528, 22961528, 22961528, 22961528, 22961528), PV = c(25871152.76,
14594952.7, 14782403.02, 14735013, 210193.81, 217361.59,
226025.93, 225898.98, 211626.67, 187911.13, 187941.88), PV_ND = c(25973196.64,
14654807.46, 14843080.44, 14795220.35, 210222.01, 217386.44,
226048.76, 225920.76, 211641.41, 187919.95, 187949.85), BROKER_R = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CREDIT_R = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), STRUCTURE_R = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), BROKER_UR_D = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), CREDIT_UR_D = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), STRUCTURE_UR_D = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), BROKER_UN_UND = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), CREDIT_UN_UND = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), STRUCTURE_UN_UND = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), .Names = c("COMMODITY", "BOOK", "STICHTAG",
"BROKERAGE", "DV", "REALIZED", "PV", "PV_ND", "BROKER_R", "CREDIT_R",
"STRUCTURE_R", "BROKER_UR_D", "CREDIT_UR_D", "STRUCTURE_UR_D",
"BROKER_UN_UND", "CREDIT_UN_UND", "STRUCTURE_UN_UND"), sorted = c("COMMODITY",
"BOOK", "STICHTAG"), class = c("data.table", "data.frame"), row.names = c(NA,
-11L), .internal.selfref = <pointer: 0x014024a0>)