ghoost2010 ghoost2010 - 3 months ago 15
R Question

how to split data frame by time interval

I have two data frames, first is the daily return of 3 securities, second is the weights of the securities, as the following:

daily.return <- data.frame(date = seq.Date(from = as.Date("2015-01-01"),
by = "days",
length.out = 100),
a = runif(100,-0.1,0.1),
b = runif(100,-0.1,0.1),
c = runif(100,-0.1,0.1))
weights <- data.frame(startDate = c(as.Date("2015-01-01"),
as.Date("2015-02-10"),
as.Date("2015-03-15")),
endDate = c(as.Date("2015-02-09"),
as.Date("2015-03-14"),
as.Date("2015-04-10")),
a = c(0.3,0.5,0.2),
b = c(0.4,0.2,0.1),
c = c(0.3,0.3,0.7)
)


I know how to split data fame by weeks etc.., if we convert data frame to xts;but how to split this daily.return according to startDate and endDate in weights?
Suppose a fund have this three securities,how to calculate the fund nav and daily return?

Answer

This should do the job.

daily.return <- data.frame(date = seq.Date(from = as.Date("2015-01-01"),
                                           by = "days",
                                           length.out = 100),
                           a = runif(100,-0.1,0.1),
                           b = runif(100,-0.1,0.1),
                           c = runif(100,-0.1,0.1))
weights <- data.frame(startDate = c(as.Date("2015-01-01"),
                                    as.Date("2015-02-10"),
                                    as.Date("2015-03-15")),
                      endDate = c(as.Date("2015-02-09"),
                                  as.Date("2015-03-14"),
                                  as.Date("2015-04-10")),
                      a = c(0.3,0.5,0.2),
                      b = c(0.4,0.2,0.1), 
                      c = c(0.3,0.3,0.7)         
)

library(quantmod)

daily.xts <- as.xts(daily.return[,-1],daily.return[,1])

# Assuming that the total period is the same in both the data frames
weights.xts <- xts(matrix(NA,nrow(daily.xts),3),order.by=index(daily.xts))
names(weights.xts) <- c("a","b","c")

for (i in 1:nrow(weights)){

  temp.inputs <- weights[i,]
  temp.period <- paste(temp.inputs[,1],temp.inputs[,2],sep="/")
  len <- nrow(weights.xts[temp.period])
  weights.xts[temp.period,1:3] <- matrix(rep(as.numeric(temp.inputs[,3:5]),len),len,byrow=T)

}

weighted.returns <- daily.xts * weights.xts
weighted.returns <- as.xts(rowSums(weighted.returns),index(weighted.returns))
names(weighted.returns) <- "Weighted Returns"
weighted.returns$Cumulative <- cumsum(weighted.returns)

plot(weighted.returns$Cumulative)

enter image description here