如何在使用do.call内部的rbind时避免重命名行?

13
我正在尝试绑定列表中元素的某些子元素。
列表OC如下:
> library(quantmod)
> OC <- getOptionChain('AAPL', NULL)

> str(OC)
List of 9
 $ Feb 2013:List of 3
  ..$ calls :'data.frame':  35 obs. of  7 variables:
  .. ..$ Strike: num [1:35] 380 390 400 410 420 430 440 445 450 455 ...
  .. ..$ Last  : num [1:35] 89.9 86 60 49.5 39.8 ...
  .. ..$ Chg   : num [1:35] 0 0 -0.4 -4.4 -0.7 -1.9 -0.55 -0.7 -0.95 -1 ...
  .. ..$ Bid   : num [1:35] 79.5 69.8 59.8 49.8 39.6 ...
  .. ..$ Ask   : num [1:35] 80.2 70.2 60.2 50.2 40.2 ...
  .. ..$ Vol   : num [1:35] 1 1 48 11 61 ...
  .. ..$ OI    : num [1:35] 2 2 55 29 41 ...
  ..$ puts  :'data.frame':  40 obs. of  7 variables:
  .. ..$ Strike: num [1:40] 380 385 390 395 400 405 410 415 420 425 ...
  .. ..$ Last  : num [1:40] 0.01 0.05 0.07 0.08 0.03 0.04 0.02 0.04 0.06 0.06 ...
  .. ..$ Chg   : num [1:40] -0.03 0 0 0 -0.08 -0.06 -0.1 -0.08 -0.11 -0.17 ...
  .. ..$ Bid   : num [1:40] NA 0.01 NA NA 0.02 0.01 0.01 0.04 0.05 0.06 ...
  .. ..$ Ask   : num [1:40] 0.01 0.02 0.03 0.03 0.03 0.04 0.06 0.06 0.07 0.09 ...
  .. ..$ Vol   : num [1:40] 15 122 1 117 186 ...
  .. ..$ OI    : num [1:40] 120 99 638 95 1319 ...
  ..$ symbol: chr "AAPL"
 $ Mar 2013:List of 3
  ..$ calls :'data.frame':  221 obs. of  7 variables:
  .. ..$ Strike: num [1:221] 255 265 
##.............truncated manually for post...........

我正在对OC列表中每个元素内的puts数据框进行基本的rbind操作,

> allputs <- do.call('rbind', lapply(OC, FUN = function(x) x$puts))
> head(allputs)
                             Strike Last   Chg  Bid  Ask Vol   OI
Feb 2013.AAPL130222P00380000    380 0.01 -0.03   NA 0.01  15  120
Feb 2013.AAPL130222P00385000    385 0.05  0.00 0.01 0.02 122   99
Feb 2013.AAPL130222P00390000    390 0.07  0.00   NA 0.03   1  638
Feb 2013.AAPL130222P00395000    395 0.08  0.00   NA 0.03 117   95
Feb 2013.AAPL130222P00400000    400 0.03 -0.08 0.02 0.03 186 1319
Feb 2013.AAPL130222P00405000    405 0.04 -0.06 0.01 0.04   1   76

然而,每个行名称都会以其父元素的名称为前缀。有没有方法可以避免这种情况?

我尝试将rbinddeparse.level = 0设置为0,但结果并不是我想要的。

> allputs <- do.call('rbind', list(lapply(OC, FUN = function(x) x$puts), deparse.level=0))
> head(allputs)
     Feb 2013 Mar 2013 Apr 2013 May 2013 Jun 2013 Jul 2013 Oct 2013 Jan 2014 Jan 2015
[1,] List,7   List,7   List,7   List,7   List,7   List,7   List,7   List,7   List,7  

> str(allputs[1])
List of 1
 $ :'data.frame':   40 obs. of  7 variables:
  ..$ Strike: num [1:40] 380 385 390 395 400 405 410 415 420 425 ...
  ..$ Last  : num [1:40] 0.01 0.05 0.07 0.08 0.03 0.04 0.02 0.04 0.06 0.06 ...
  ..$ Chg   : num [1:40] -0.03 0 0 0 -0.08 -0.06 -0.1 -0.08 -0.11 -0.17 ...
  ..$ Bid   : num [1:40] NA 0.01 NA NA 0.02 0.01 0.01 0.04 0.05 0.06 ...
  ..$ Ask   : num [1:40] 0.01 0.02 0.03 0.03 0.03 0.04 0.06 0.06 0.07 0.09 ...
  ..$ Vol   : num [1:40] 15 122 1 117 186 ...
  ..$ OI    : num [1:40] 120 99 638 95 1319 ...
> str(allputs[2])
List of 1
 $ :'data.frame':   207 obs. of  7 variables:
  ..$ Strike: num [1:207] 255 260 265 270 275 280 285 290 295 300 ...
  ..$ Last  : num [1:207] 0.08 0.03 0.06 0.01 0.03 0.1 0.02 0.02 0.05 0.02 ...
  ..$ Chg   : num [1:207] 0 0.02 0 0 0 0 0 0 0 0 ...
  ..$ Bid   : num [1:207] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Ask   : num [1:207] 0.02 0.01 0.02 0.02 0.02 0.02 0.02 0.03 0.03 0.03 ...
  ..$ Vol   : num [1:207] 5 30 5 10 3 6 1 10 5 2 ...
  ..$ OI    : num [1:207] 33 668 541 512 455 ...

3
在基础 R 中执行此操作的方法是将传递给 rbind 的列表使用 unname 函数进行去除名称处理:do.call('rbind', unname(lapply(OC, FUN = function(x) x$puts))) - Zelazny7
1个回答

14
你可以通过使用data.table::rbindlist来避免do.call(rbind,...)。这将返回一个data.tabledata.tables没有行名称。同时,它也非常快速!
library(data.table)
allputs <- rbindlist(lapply(OC, FUN = function(x) x$puts))
# my eyes, I'm blinded!

如果您想将原始行名称包含为列,则

lputs <- lapply(OC, FUN = function(x) x$puts)


 allputs <- rbindlist(lputs)
 # add the column with rownames
 allputs[,rn := unlist(lapply(lputs, rownames))]

如果你不想转移到data.tables,那么你可以将父级名称设置为NULL

 names(lputs) <- NULL

 do.call('rbind', lputs)

这里的注意事项是rbindlist已经期望每个项目都是一个统一的列表、数据框或数据表。有没有办法使用do.callargs参数来传递rbind一个row.names = F或其他东西? - d8aninja
4
你可以使用do.call(rbind, c(yourlist, make.row.names = FALSE));参见?do.call中的第二个例子:“如果我们已经有一个列表[...],我们需要使用c()来添加进一步的参数”。 - Henrik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接