问题描述
我正在尝试绑定列表中元素的某些子元素
I am trying to bind some sub elements of the elements from the list
列表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
I am doing basic rbind of all the puts
dataframe inside each list element of OC,
> 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
但是,每个行名之前都带有其父元素的名称.有办法避免这种情况吗?
我尝试将deparse.level = 0
设置为rbind
,但是结果不是我想要的..
I tried setting deparse.level = 0
for rbind
, but result is not what I want..
> 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 ...
推荐答案
您可以使用data.table::rbindlist
来避免do.call(rbind,...)
.
这将返回data.table
. data.tables
没有行名.
This will return a data.table
. data.tables
don't have rownames.
它也快得让人眼花!乱!
It is also blindingly fast!
library(data.table)
allputs <- rbindlist(lapply(OC, FUN = function(x) x$puts))
# my eyes, I'm blinded!
如果您想将原始行名包含在列中,则
If you want to include the original rownames as a column then
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
If you don't want to move to data.tables, then you could set the parent names to NULL
names(lputs) <- NULL
do.call('rbind', lputs)
这篇关于在do.call中使用rbind时如何避免重命名行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!