如何按时间间隔分割数据帧

时间:2016-09-06 05:08:32

标签: r split portfolio

我有两个数据框,第一个是3个证券的每日回报,第二个是证券的权重,如下:

    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)         
                             )

我知道如何将数据分成几周等等,如果我们将数据帧转换为xts;但是如何将其每日拆分。根据startDate和endDate的权重返回? 假设一只基金有这三种证券,如何计算基金导航和每日回报?

2 个答案:

答案 0 :(得分:1)

这应该可以胜任。

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

答案 1 :(得分:0)

您可以使用daily.return根据权重的开始和结束日期拆分apply,执行逐行操作

apply(weights, 1, function(x) daily.return[daily.return$date >= x[1]
                                                  & daily.return$date <= x[2], ])

这将根据weights中的范围提供3个数据帧的列表。

修改

如果我理解正确,您希望a的{​​{1}},bc列中的每个值与{{1}中的相应列相乘}}。

daily.return