使用列名称向量减少列表

问题描述 投票:0回答:1

我有以下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,]我怎么能这样做?

r iteration lapply xts
1个回答
1
投票

你反对使用for循环吗?他们倾向于在概念上简化这些问题而无需更高级的列表功能应用程序

lists <- list(cbind(MSFT = rnorm(4), AMZN = rnorm(4), AAPL = rnorm(4), JPM = rnorm(4)),
     cbind(MSFT = rnorm(4), AMZN = rnorm(4), AAPL = rnorm(4), JPM = rnorm(4)))


standin1 <- cbind(c("AAPL","JPM"), c("MSFT","AMZN"))

n <- nrow(standin1)
subsetlist <- vector("list",n) # initialize
for(i in 1:n) subsetlist[[i]] <- lists[[i]][, standin1[i,]]
subsetlist

编辑:

我正在使用mapply同时循环两个列表。我们只需将standin1的格式更改为list(c(A,B),c(C,D))而不是矩阵:

reformatted_standin1 <- as.list(as.data.frame(t(standin1), stringsAsFactors = F))
mapply(function(L,x) list(L[,x]), split_prices_tbl3, reformatted_standin1)
© www.soinside.com 2019 - 2024. All rights reserved.