[英]Create vectors by extracting elements from list in R
I have a list object with components that look like the following:我有一个列表对象,其组件如下所示:
$AA
(Intercept) AA
3 4
$CD
(Intercept) CD
4 2
$XY
(Intercept) XY
5 3
I would like a dataframe that looks like the following.我想要一个如下所示的数据框。 Alpha corresponds to (Intercept) above, and Beta corresponds to the value represented by the name of the list item.
Alpha对应上面的(Intercept),Beta对应列表项名称所代表的值。
Name Alpha Beta
AA 3 4
CD 4 2
XY 5 3
I know I can extract values in the following way, where coefs is the name of the list.我知道我可以通过以下方式提取值,其中 coefs 是列表的名称。
coefs$AA[1] #value for Alpha
coefs$AA[2] #value for Beta
How can I do this for all components of the list?如何为列表的所有组件执行此操作? In case it is helpful, the list represents the coefficients alpha and beta from the output of several regressions.
如果有帮助,该列表表示来自多个回归输出的系数 alpha 和 beta。
Data:数据:
dput(coefs)
list(AA = c(`(Intercept)` = 0.00781534742097672, AA = 0.140455432370463
), ALD = c(`(Intercept)` = 0.00557182631277353, ALD = 0.198916138245909
), AXP = c(`(Intercept)` = 0.00633440947245968, AXP = 0.154322769323201
), BA = c(`(Intercept)` = 0.00834280894501586, BA = 0.0941536953127183
), CAT = c(`(Intercept)` = 0.00655105648795371, CAT = 0.137462558039722
), CHV = c(`(Intercept)` = 0.0058599522637348, CHV = 0.269888033479484
), DD = c(`(Intercept)` = 0.00623849818068288, DD = 0.231206114050254
), DIS = c(`(Intercept)` = 0.0067614536257978, DIS = 0.180513015485431
), EK = c(`(Intercept)` = 0.00790519763382617, EK = 0.118639013243976
), ENE = c(`(Intercept)` = 0.00781778892551527, ENE = 0.10858254068385
), GE = c(`(Intercept)` = 0.00294196299492578, GE = 0.364461751377626
), GM = c(`(Intercept)` = 0.0085174289624691, GM = 0.0873909980420347
), GT = c(`(Intercept)` = 0.00707641628728448, GT = 0.204555410056181
), HWP = c(`(Intercept)` = 0.00634667846671781, HWP = 0.137844745901337
), IBM = c(`(Intercept)` = 0.00849044794002662, IBM = 0.0964252010051216
), IP = c(`(Intercept)` = 0.00906399011873481, IP = 0.151009935502848
), JNJ = c(`(Intercept)` = 0.00715601895187441, JNJ = 0.219180395262686
), JPM = c(`(Intercept)` = 0.00741476793645128, JPM = 0.241375375114064
), KO = c(`(Intercept)` = 0.00660478201497359, KO = 0.18143193126914
), MCD = c(`(Intercept)` = 0.00666717322231, MCD = 0.195659962104338
), MMM = c(`(Intercept)` = 0.00548410132538106, MMM = 0.331060750102674
), MO = c(`(Intercept)` = 0.00811094805656566, MO = 0.147779503536321
), MRK = c(`(Intercept)` = 0.00754935866336214, MRK = 0.237287608850202
), PG = c(`(Intercept)` = 0.00575628655569729, PG = 0.244866242495482
), S = c(`(Intercept)` = 0.00565228048065954, S = 0.175250230655151
), TRV = c(`(Intercept)` = 0.00392956375771792, TRV = 0.199917956865273
), UK = c(`(Intercept)` = 0.00627252118663475, UK = 0.113058380208163
), UTX = c(`(Intercept)` = 0.00417151421589462, UTX = 0.308220734271536
), WMT = c(`(Intercept)` = 0.0101499747347963, WMT = 0.1250276376077
), XON = c(`(Intercept)` = 0.0047869774616443, XON = 0.42015961399732
))
Here's an approach with do.call
:这是
do.call
的一种方法:
Result <- data.frame(Name = names(coefs),
do.call(rbind, coefs))
names(Result) <- c("Name","Alpha","Beta")
rownames(Result) <- NULL
Result
# Name Alpha Beta
#1 AA 0.007815347 0.1404554
#2 ALD 0.005571826 0.1989161
#3 AXP 0.006334409 0.1543228
#4 BA 0.008342809 0.0941537
#5 CAT 0.006551056 0.1374626
#...
Using purrr
's map_df
:使用
purrr
的map_df
:
purrr::map_df(coefs, ~setNames(data.frame(t(.x)), c('alpha', 'beta')), .id = 'Name')
# Name alpha beta
#1 AA 0.007815347 0.1404554
#2 ALD 0.005571826 0.1989161
#3 AXP 0.006334409 0.1543228
#4 BA 0.008342809 0.0941537
#5 CAT 0.006551056 0.1374626
#6 CHV 0.005859952 0.2698880
#...
#...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.