• R语言
  • 为啥结果没有按天拼接上去啊。。。

背景:郑州商品交易所的tick数据为每天成交量和成交金额累积记录。
需求:将一段时间的tick数据按天切割后对每天的成交量和成交金额做差分,分离出tick数据。tick数据即为一小段时间,如1秒内的累积成交量和累积金额。由于时间间隔很短,近似认为平均价格是单笔成交价格。
问题:下面代码运行后,没有按预期的把每天的tick数据差分后拼接在结果变量里,而是只输出了最后一天的数据。不知道哪里出问题了。。。。
数据:示例数据

# this script is used to separate accumulated contracts into single transactions
# first column should be TradingDay
# second column should be Volume, and third Turnover

func <- function(x)
{

# get trading days and number of trading days

    n <- unique(x[, 1])
    j <- length(n)

# sort data by date and volume

    x <- arrange(x, x[,1], x[,2])
    i <- 1
    result <- data.frame(matrix(ncol = 6, nrow = 1))
    names(result)[1:6] <- c("TradingDay", "Volume", "Turnover", "UpdateTime", "vol_dif", "turn_dif")
    for (i in j){
        day <- subset(x, x[, 1] == n[i])
        vol_dif <- diff(day[, 2])
        turn_dif <- diff(day[, 3])
        c <- cbind(vol_dif, turn_dif)
        tem <- c(day[1, 2], day[1, 3])
        c <- rbind(tem, c)
        day <- cbind(day, c)
        result <- rbind(result, day)
        i <- i + 1
        }

# remove data without change in volume and turnover, which means there are only
# changes in bid/ask price and volume
        result <- subset(result, vol_dif != 0 & turn_dif != "NA")
        result <- transform(result, averageprice = turn_dif / vol_dif)
        return(result)
}