按日期按降序排序数据时遇到问题

时间:2017-02-10 00:43:52

标签: r

我正在使用R来了解资本资产定价模型。我正在输入来自雅虎财务的历史数据,默认情况下,按日期设置为升序。我正在使用订单功能,但它似乎没有工作。这是我的代码:

    #This is the pre-processing for historical stock prices against returns of another stock(usually NASDAQ)
frmkt.returns <- function (file1, file2){
  #input downloaded files from excel and convert to workable format
  file.1 <- read.table(file1,header = TRUE, sep = ",")[,c("Date","Adj.Close")]
  file.2 <- read.table(file2,header = TRUE, sep = ",")[,c("Date","Adj.Close")]
  file.1 <- merge(file.1, file.2, by = "Date")
  file.1[,c("Date")] <- as.Date(file.1[,c("Date")])
  file.1 <- file.1[rev(order(file.1$Date)),]

  #Perform operations to get rates of returns on stocks
  file.1[-nrow(file.1),-1] <- file.1[-nrow(file.1),-1]/ file.1[-1,-1]-1
  file.1 <- file.1[-nrow(file.1),]

  #Input 5 year Treasurey Bond as referrence to returns of free market
  #Convert to workable format and merge with Stock returns object
  tbonds <- read.table("/Users/bhumphreys/Downloads/fiveYR_treasurey.csv", header = TRUE, sep = ",")[,c("Date","Adj.Close")]
  names(tbonds)[2] <- "tbonds.returns"
  tbonds[,c("Date")] <- as.Date(tbonds[,c("Date")])
  file.1 <- merge(file.1, tbonds, by= "Date")
  file.1$tbonds.returns <- file.1$tbonds.returns/100
  names(file.1)[2:3] <- c("stock.returns", "nasdaq.returns")
  file.1[,c("stock.returns","nasdaq.returns")] <- file.1[,c("stock.returns", "nasdaq.returns")]- file.1[,"tbonds.returns"]

  return(file.1)
}

这是我一直得到的输出:

frmkt.returns(xomFile,snpFile)
         Date stock.returns nasdaq.returns tbonds.returns
1  2012-02-01  3.136297e-02   2.957670e-02        0.00725
2  2012-03-01 -6.330922e-03   2.877178e-02        0.00899
3  2012-04-02 -1.470687e-02   2.250261e-02        0.01021
4  2012-05-01 -9.137423e-02  -1.766622e-02        0.00835
5  2012-06-01  8.206149e-02  -9.709351e-02        0.00620
6  2012-07-02  8.268583e-03   6.175071e-02        0.00669
7  2012-08-01  5.283751e-03   7.540821e-04        0.00643
8  2012-09-04  4.131724e-02   1.531680e-02        0.00622
9  2012-10-01 -9.261786e-03   2.195070e-02        0.00620
10 2012-11-01 -3.446171e-02  -1.897965e-02        0.00728
11 2012-12-03 -2.431944e-02  -1.897973e-02        0.00628
12 2013-01-02  3.188471e-02   2.994473e-02        0.00763
13 2013-02-01 -7.079322e-03   2.593275e-02        0.00877
14 2013-03-01 -1.226536e-03  -4.155914e-03        0.00748
15 2013-04-01 -2.000930e-02   2.138199e-02        0.00758
16 2013-05-01  1.711385e-02   6.591916e-03        0.00655
17 2013-06-03 -1.164647e-02   2.614938e-02        0.01032
18 2013-07-01  2.367151e-02  -2.948047e-02        0.01396
19 2013-08-01 -7.886746e-02   4.191165e-02        0.01500
20 2013-09-03 -2.956996e-02  -5.603171e-02        0.01672
21 2013-10-01  2.738862e-02   1.946154e-02        0.01422
22 2013-11-01  3.643796e-02   2.558564e-02        0.01373
23 2013-12-02  6.837443e-02   8.076057e-03        0.01421
24 2014-01-02 -1.064880e-01   9.801299e-05        0.01716
25 2014-02-03  3.762477e-02  -6.354628e-02        0.01437
26 2014-03-03  3.638218e-05   4.500339e-02        0.01461
27 2014-04-01  3.102342e-02   4.157888e-03        0.01740
28 2014-05-01 -2.822288e-02  -1.750584e-02        0.01653
29 2014-06-02 -1.446795e-02   5.959814e-03        0.01596
30 2014-07-01 -3.384238e-02   8.557262e-03        0.01656
31 2014-08-01 -4.471628e-03  -4.114060e-02        0.01673
32 2014-09-02 -7.125370e-02   2.320441e-02        0.01686
33 2014-10-01  1.145283e-02  -4.485805e-02        0.01683
34 2014-11-03 -7.337974e-02   2.047610e-02        0.01634
35 2014-12-01  5.885659e-03   2.447699e-03        0.01521
36 2015-01-02 -7.058784e-02  -1.386193e-02        0.01618
37 2015-02-02  8.585158e-03  -2.999691e-02        0.01185
38 2015-03-02 -5.576196e-02   3.199194e-02        0.01578
39 2015-04-01  1.462245e-02  -4.051051e-02        0.01326
40 2015-05-01 -3.184140e-02   8.525832e-03        0.01507
41 2015-06-01 -3.903412e-02  -1.392837e-02        0.01556
42 2015-07-01 -6.497674e-02  -3.326737e-02        0.01702
43 2015-08-03 -5.637230e-02  -5.214170e-03        0.01514
44 2015-09-01 -2.686886e-02  -1.028315e-01        0.01504
45 2015-10-01  9.914473e-02  -8.490621e-03        0.01370
46 2015-11-02 -2.008928e-02   7.804346e-02        0.01564
47 2015-12-01 -6.139235e-02  -1.663497e-02        0.01596
48 2016-01-04 -1.863287e-02  -6.013920e-02        0.01735
49 2016-02-01  2.505843e-02  -5.023954e-02        0.01383
> 

我为冗长的代码道歉,但正如您所看到的,数据没有从默认值更改。我也试过了 file.1 <- file.1[order(file.1$Date, decreasing = TRUE),]

请帮我扭转这些数据。

2 个答案:

答案 0 :(得分:0)

使用lubridate包更改日期格式,然后订购。

#Get your data

df2 <- read.table(text = 
' Date stock.returns nasdaq.returns tbonds.returns
1  2012-02-01  3.136297e-02   2.957670e-02        0.00725
2  2012-03-01 -6.330922e-03   2.877178e-02        0.00899
3  2012-04-02 -1.470687e-02   2.250261e-02        0.01021
4  2012-05-01 -9.137423e-02  -1.766622e-02        0.00835
5  2012-06-01  8.206149e-02  -9.709351e-02        0.00620
6  2012-07-02  8.268583e-03   6.175071e-02        0.00669
7  2012-08-01  5.283751e-03   7.540821e-04        0.00643
8  2012-09-04  4.131724e-02   1.531680e-02        0.00622
9  2012-10-01 -9.261786e-03   2.195070e-02        0.00620
10 2012-11-01 -3.446171e-02  -1.897965e-02        0.00728
11 2012-12-03 -2.431944e-02  -1.897973e-02        0.00628
12 2013-01-02  3.188471e-02   2.994473e-02        0.00763
13 2013-02-01 -7.079322e-03   2.593275e-02        0.00877
14 2013-03-01 -1.226536e-03  -4.155914e-03        0.00748
15 2013-04-01 -2.000930e-02   2.138199e-02        0.00758
16 2013-05-01  1.711385e-02   6.591916e-03        0.00655
17 2013-06-03 -1.164647e-02   2.614938e-02        0.01032
18 2013-07-01  2.367151e-02  -2.948047e-02        0.01396
19 2013-08-01 -7.886746e-02   4.191165e-02        0.01500
20 2013-09-03 -2.956996e-02  -5.603171e-02        0.01672
21 2013-10-01  2.738862e-02   1.946154e-02        0.01422
22 2013-11-01  3.643796e-02   2.558564e-02        0.01373
23 2013-12-02  6.837443e-02   8.076057e-03        0.01421
24 2014-01-02 -1.064880e-01   9.801299e-05        0.01716
25 2014-02-03  3.762477e-02  -6.354628e-02        0.01437
26 2014-03-03  3.638218e-05   4.500339e-02        0.01461
27 2014-04-01  3.102342e-02   4.157888e-03        0.01740
28 2014-05-01 -2.822288e-02  -1.750584e-02        0.01653
29 2014-06-02 -1.446795e-02   5.959814e-03        0.01596
30 2014-07-01 -3.384238e-02   8.557262e-03        0.01656
31 2014-08-01 -4.471628e-03  -4.114060e-02        0.01673
32 2014-09-02 -7.125370e-02   2.320441e-02        0.01686
33 2014-10-01  1.145283e-02  -4.485805e-02        0.01683
34 2014-11-03 -7.337974e-02   2.047610e-02        0.01634
35 2014-12-01  5.885659e-03   2.447699e-03        0.01521
36 2015-01-02 -7.058784e-02  -1.386193e-02        0.01618
37 2015-02-02  8.585158e-03  -2.999691e-02        0.01185
38 2015-03-02 -5.576196e-02   3.199194e-02        0.01578
39 2015-04-01  1.462245e-02  -4.051051e-02        0.01326
40 2015-05-01 -3.184140e-02   8.525832e-03        0.01507
41 2015-06-01 -3.903412e-02  -1.392837e-02        0.01556
42 2015-07-01 -6.497674e-02  -3.326737e-02        0.01702
43 2015-08-03 -5.637230e-02  -5.214170e-03        0.01514
44 2015-09-01 -2.686886e-02  -1.028315e-01        0.01504
45 2015-10-01  9.914473e-02  -8.490621e-03        0.01370
46 2015-11-02 -2.008928e-02   7.804346e-02        0.01564
47 2015-12-01 -6.139235e-02  -1.663497e-02        0.01596
48 2016-01-04 -1.863287e-02  -6.013920e-02        0.01735
49 2016-02-01  2.505843e-02  -5.023954e-02        0.01383', header = TRUE)

# Convert the date to required format I am assuming it is ymd format     
df2 <- df2[order(df2$Date, decreasing = TRUE), , drop = FALSE]

# order the data according to date in descending order
df2 <- df2[order(df2$Date, decreasing = TRUE), , drop = FALSE]

答案 1 :(得分:0)

目前,您通过降序日期在订单tbonds后第二次与file.1合并。默认情况下,mergeby列排序:

考虑在第二次合并时不指定排序:

file.1 <- merge(file.1, tbonds, by= "Date", sort=FALSE)

或者,在所有合并后移动降序:

file.1 <- merge(file.1, tbonds, by= "Date")
file.1 <- file.1[order(file.1$Date, decreasing = TRUE),]