2016-04-25 96 views
0
   SCA-B.ST.Open SCA-B.ST.High SCA-B.ST.Low SCA-B.ST.Close BOUGHT 
2008-01-14  104.50  105.00   101.50   102.75 
2008-01-15  102.50  102.50   98.25   99.50 

我已經由包含當我進入的位置處與股票的在R.一些股票信息,我也取得了手動列表(在紙上)的XTS對象包含值「B」或「S」。更新列值

如何根據XTS對象提供的日期索引將「B」或「S」值插入到「BOUGHT」列中?

預先感謝您!

回答

1

你可以簡單地做(在​​這裏我假設你的XTS被稱爲data):

# add col with 'NA' values 
data$BOUGHT <- NA 
data[as.Date("2008-01-15"),"BOUGHT"] <- "B" 
# check/print 
data[as.Date("2008-01-15"),"BOUGHT"] 

輸出:

> # add col with 'NA' values 
> data$BOUGHT <- NA 
> data[as.Date("2008-01-15"),"BOUGHT"] <- "B" 
> # check/print 
> data[as.Date("2008-01-15"),"BOUGHT"] 
      BOUGHT 
2008-01-15 "B" 
+0

這完美地工作!謝謝! –