我有以下code。
standin1<-cbind(c("AAPL","JPM"), c("MSFT","AMZN"))
wantedstocks<-unique(c(standin1))
wantedstockprices<-tq_get(wantedstocks,"stock.prices", from = "2010-01-01", to = "2012-01-01")
prices_tbl1<-split(wantedstockprices,wantedstockprices$symbol)
prices_tbl2<-lapply(prices_tbl1,function(x) xts(x$adjusted,x$date))
prices_tbl3<-do.call(merge,prices_tbl2) #combine xts objects
log_return_allcolumns = do.call(merge.xts,lapply(colnames(prices_tbl3),function(x){
z = periodReturn(prices_tbl3[,x],period = "daily",type="log");
colnames(z) = x;
return(z)
} ))
split_prices_tbl3<-split.xts(log_return_allcolumns,f="years",k=1)
remove_first_row<-lapply(split_prices_tbl3, function(x) x[-1,])
head(remove_first_row)
[[1]]
AMZN JPM MSFT AAPL
2010-01-05 5.882649e-03 0.0191843964 0.0003230544 0.0017274487
2010-01-06 -1.828179e-02 0.0054797151 -0.0061562747 -0.0160339668
2010-01-07 -1.715962e-02 0.0196148156 -0.0104541501 -0.0018506625
2010-01-08 2.671686e-02 -0.0024586846 0.0068731050 0.0066263094
2010-01-11 -2.433510e-02 -0.0033633260 -0.0128016544 -0.0088605388
[ reached getOption("max.print") -- omitted 246 rows ]
[[2]]
AMZN JPM MSFT AAPL
2011-01-04 0.0042791497 0.0143687817 3.923736e-03 5.205323e-03
2011-01-05 0.0129422264 0.0121542431 -3.209453e-03 8.146719e-03
2011-01-06 -0.0083583695 -0.0049335851 2.886507e-02 -8.085237e-04
2011-01-07 -0.0019927083 -0.0190659530 -7.662873e-03 7.135931e-03
2011-01-10 -0.0043764395 -0.0055143151 -1.337576e-02 1.865725e-02
我想要的是减少输出,因此我只得到具有匹配名称standin1
的列表。因此,在第一个时期remove_first_row[1]
中,我希望来自standin1[1,]
和remove_first_now[2]
的匹配搅动的输出,而我想要具有来自standin1[2,]
的匹配字符串的输出。所以所需的输出是
$`1`
AAPL MSFT
2010-01-05 0.0017274487 0.0003230544
2010-01-06 -0.0160339668 -0.0061562747
2010-01-07 -0.0018506625 -0.0104541501
2010-01-08 0.0066263094 0.0068731050
2010-01-11 -0.0088605388 -0.0128016544
$`2`
JPM AMZN
2011-01-04 0.0143687817 0.0042791497
2011-01-05 0.0121542431 0.0129422264
2011-01-06 -0.0049335851 -0.0083583695
2011-01-07 -0.0190659530 -0.0019927083
2011-01-10 -0.0055143151 -0.0043764395
我尝试过
selected_prices<-lapply(split_prices_tbl3, function(x) x[,standin1[1,]])
但是然后它不会遍历standin1[2,]
我该怎么做?