【问题标题】:Reduce a list with a vector of columns names减少具有列名称向量的列表
【发布时间】:2019-02-14 11:11:17
【问题描述】:

我有以下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】:

    你反对使用 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)
    

    【讨论】:

    • 嗨,埃文,感谢您的回答。我不能使用循环,因为它会在实际设置中产生一些其他问题,但是我同意这通常是一种更简单的方法,因为我也在努力使用列表函数应用程序。关于您的应用功能,我在每个列表中得到 2 个列表。你能重新制定它,所以我在每个列表中只收到 1 个输出。
    • 没问题 - 很高兴我能帮上忙
    • 只做Map(function(x,y)x[,y],lists,data.frame(standin1))有什么大不了的??
    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 2020-01-24
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多