使用quantmod创建新变量后出现奇怪的结果

时间:2015-07-21 16:42:30

标签: r quantmod

我编写了这行代码,以便了解IBM股票在当天之前的最后一天是否收盘涨跌。我是quantmod的新手,所以我根据我的R知识编写了这行代码但是我得到了一些奇怪的结果。出了什么问题?我得到了一个奇怪的摘要结果和重复的指示。 以下是我的代码行:

> IBM_TRAIN$LastDay_green<- ifelse((lag(IBM_TRAIN$IBM.Open,k=1) < lag(IBM_TRAIN$IBM.Close,k=1)),1,0)
> summary(IBM_TRAIN$LastDay_green)
     Index            LastDay_green   
 Min.   :2007-01-03   Min.   :0.0000  
 1st Qu.:2008-10-15   1st Qu.:0.0000  
 Median :2010-07-21   Median :1.0000  
 Mean   :2010-07-21   Mean   :0.5418  
 3rd Qu.:2012-04-24   3rd Qu.:1.0000  
 Max.   :2014-01-30   Max.   :1.0000  
                      NA's   :1769    
Warning message:
In data.row.names(row.names, rowsi, i) :
  some row.names duplicated: 15,17,19,23,25,27,29,33,35,37,39,43,45,47,49,53,55,57,61,63,65,67,71,73,75,77,81,83,85,87,91,93,95,97,101,103,105,107,111,113,115,119,121,123,125,129,131,133,135,139,141,143,145,149,151,153,155,159,161,163,165,169,171,173,175,179,181,183,185,189,191,193,197,199,201,203,207,209,211,213,217,219,221,223,227,229,231,233,237,241,245,247,249,251,255,257,259,261,265,267,269,271,275,277,279,281,285,287,289,291,295,297,299,301,305,307,309,311,315,317,319,321,325,327,329,333,335,337,339,343,345,347,349,353,355,357,359,363,365,367,369,373,375,377,379,383,385,387,389,393,395,397,399,403,405,407,409,413,415,417,419,423,425,427,429,433,435,441,443,445,447,451,453,455,457,461,463,465,467,471,473,475,477,483,485,491,493,497,499,501,503,507,509,511,513,517,519,521,525,527,529,531,535,537,539,541,545,547,549,551,555,557,559,563,565,567,569,573,575,577,579,583,585,587,589,593,595,597,601,603,605,607,611,613,615,617,621,623,625,627,631,633,635,637,641,643,645,647,651,653,655,657 [... truncated]

2 个答案:

答案 0 :(得分:1)

library(quantmod)
#get stock data
getSymbols("IBM")


#Show last two days of the series
  tail(IBM, n=2)

#series will show you the change day to day. 
dailyReturn(IBM))



           daily.returns
2015-07-13  0.0145553042
2015-07-14 -0.0045460147
2015-07-15 -0.0004744796
2015-07-16  0.0146561503
2015-07-17  **0.0088303801**
2015-07-20  **0.0041157383**

答案 1 :(得分:1)

我无法发表评论,因此将其添加为答案。 索引摘要是因为第一次操作的结果是XTS。

我认为这应该只给出colums值的摘要 -

summary(coredata(IBM_TRAIN$LastDay_green) )