2013-02-20 71 views
9

我想元素的一些子元素从列表中绑定如何避免在do.call中使用rbind时重命名行?

名单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 

然而,每个rowname获取与它的名字父元素前缀。有没有办法避免这种情况?

我尝试了rbind设置deparse.level = 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 ... 
+0

在base R中执行此操作的方法是将''unname'传递给'rbind'的列表:do.call('rbind',unname(lapply(OC,FUN = function(x)x $放入)))' – Zelazny7 2017-10-19 14:56:29

回答

11

您可以通过使用data.table::rbindlist避免do.call(rbind,...)

这将返回data.tabledata.tables没有rownames。

它也非常快!

library(data.table) 
allputs <- rbindlist(lapply(OC, FUN = function(x) x$puts)) 
# my eyes, I'm blinded! 

如果要包含原始rownames为一列,然后

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) 
+0

这里的提示是'rbindlist'' alr eady希望每个项目都是一个统一的列表,data.frame或data.table'。有没有办法使用'do.call'的'args'参数来传递'rbind'一个'row.names = F'什么的? – d8aninja 2017-09-21 22:22:28

相关问题