R map() 2级进入列表
R map() 2 levels into list
我坚持做嵌套的 map() 或 map() 管道。
我在对象 "output" 中有一个包含 4 个输出的列表。在四个输出中的每一个中,都有一个元素 "parameters",它是一个包含 3 个元素的列表。第一个元素是 "unstandardized"
在查看工具中,我可以看到从任何一个输出中获取非标准化参数的代码
output[["ar.4g_gm.pr.dual..semi.inv..phantom.out"]][["parameters"]][["unstandardized"]])
我尝试使用 map over outputs 提取通过管道传输到 map_dfr 的参数来提取和绑定非标准化参数,这完成了工作...
x<- map(output,"parameters") %>% map_dfr("unstandardized")
但我想在结果的一列中包含顶级列表元素名称(即输出文件)。
有没有办法嵌套映射函数或一些其他语法来将 4 个顶级列表元素名称放入列中?
这是带有虚拟数据的语句。我工作但我需要 cbind rep(c"out1","out2","out3", each=5) 结果,我希望它发生 w/o cbind.
output <- list(out1=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(1,times=5)))),
out2=list(e1=c(3,4,5),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(2,times=5)))),
out3=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(3,times=5)))) )
output[["out1"]][["parm"]][["stand"]]
map(output,"parm") %>% map_dfr("stand")
output <- list(out1=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(1,times=5)))),
out2=list(e1=c(3,4,5),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(2,times=5)))),
out3=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(3,times=5)))) )
library(tidyverse)
map(output,"parm") %>%
map("stand") %>%
map2(names(output), ~ cbind(.x, df_name=.y))
# $out1
# V1 V2 V3 V4 V5 V6 V7 V8 grp df_name
# 1 695 356 109 463 688 496 842 310 1 out1
# 2 922 450 680 170 567 921 530 419 1 out1
# 3 568 604 626 446 364 206 541 644 1 out1
# 4 210 237 300 432 366 945 413 368 1 out1
# 5 529 224 392 181 156 126 255 283 1 out1
#
# $out2
# V1 V2 V3 V4 V5 V6 V7 V8 grp df_name
# 1 320 429 109 749 394 657 690 764 2 out2
# 2 580 296 755 101 385 582 956 547 2 out2
# 3 939 122 697 146 747 108 672 836 2 out2
# 4 550 972 128 396 874 224 158 133 2 out2
# 5 923 650 888 895 742 166 533 225 2 out2
#
# $out3
# V1 V2 V3 V4 V5 V6 V7 V8 grp df_name
# 1 347 928 777 656 503 783 847 620 3 out3
# 2 496 586 919 991 810 797 779 202 3 out3
# 3 644 731 441 896 284 514 954 981 3 out3
# 4 303 803 945 806 938 692 587 775 3 out3
# 5 243 666 719 823 133 773 585 461 3 out3
library(purrr)
library(dplyr)
map(output, pluck, "parm", "stand") %>%
bind_rows(.id = "foo")
# foo V1 V2 V3 V4 V5 V6 V7 V8 grp
# 1 out1 845 527 296 902 358 447 317 347 1
# 2 out1 679 473 290 482 349 691 144 731 1
# 3 out1 842 574 135 894 628 542 757 174 1
# 4 out1 379 548 836 176 796 744 889 922 1
# 5 out1 498 837 492 965 255 508 138 689 1
# 6 out2 203 599 158 355 793 884 722 210 2
# 7 out2 543 693 484 195 511 174 793 654 2
# 8 out2 593 839 296 926 387 788 260 143 2
# 9 out2 373 363 323 939 416 348 792 211 2
# 10 out2 773 218 616 806 119 304 775 775 2
# 11 out3 171 217 859 899 664 737 114 837 3
# 12 out3 953 225 600 581 528 388 714 899 3
# 13 out3 615 550 860 134 667 136 987 993 3
# 14 out3 494 407 726 128 559 418 782 832 3
# 15 out3 729 734 432 354 716 288 734 264 3
我坚持做嵌套的 map() 或 map() 管道。
我在对象 "output" 中有一个包含 4 个输出的列表。在四个输出中的每一个中,都有一个元素 "parameters",它是一个包含 3 个元素的列表。第一个元素是 "unstandardized"
在查看工具中,我可以看到从任何一个输出中获取非标准化参数的代码
output[["ar.4g_gm.pr.dual..semi.inv..phantom.out"]][["parameters"]][["unstandardized"]])
我尝试使用 map over outputs 提取通过管道传输到 map_dfr 的参数来提取和绑定非标准化参数,这完成了工作...
x<- map(output,"parameters") %>% map_dfr("unstandardized")
但我想在结果的一列中包含顶级列表元素名称(即输出文件)。
有没有办法嵌套映射函数或一些其他语法来将 4 个顶级列表元素名称放入列中?
这是带有虚拟数据的语句。我工作但我需要 cbind rep(c"out1","out2","out3", each=5) 结果,我希望它发生 w/o cbind.
output <- list(out1=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(1,times=5)))),
out2=list(e1=c(3,4,5),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(2,times=5)))),
out3=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(3,times=5)))) )
output[["out1"]][["parm"]][["stand"]]
map(output,"parm") %>% map_dfr("stand")
output <- list(out1=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(1,times=5)))),
out2=list(e1=c(3,4,5),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(2,times=5)))),
out3=list(e1=c(1,2,3),
e2=c(T,F,T),
parm=list(a = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
b = as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),
stand = cbind(as.data.frame(matrix(sample(101:999,size=40,replace=TRUE),nrow=5)),grp=rep(3,times=5)))) )
library(tidyverse)
map(output,"parm") %>%
map("stand") %>%
map2(names(output), ~ cbind(.x, df_name=.y))
# $out1
# V1 V2 V3 V4 V5 V6 V7 V8 grp df_name
# 1 695 356 109 463 688 496 842 310 1 out1
# 2 922 450 680 170 567 921 530 419 1 out1
# 3 568 604 626 446 364 206 541 644 1 out1
# 4 210 237 300 432 366 945 413 368 1 out1
# 5 529 224 392 181 156 126 255 283 1 out1
#
# $out2
# V1 V2 V3 V4 V5 V6 V7 V8 grp df_name
# 1 320 429 109 749 394 657 690 764 2 out2
# 2 580 296 755 101 385 582 956 547 2 out2
# 3 939 122 697 146 747 108 672 836 2 out2
# 4 550 972 128 396 874 224 158 133 2 out2
# 5 923 650 888 895 742 166 533 225 2 out2
#
# $out3
# V1 V2 V3 V4 V5 V6 V7 V8 grp df_name
# 1 347 928 777 656 503 783 847 620 3 out3
# 2 496 586 919 991 810 797 779 202 3 out3
# 3 644 731 441 896 284 514 954 981 3 out3
# 4 303 803 945 806 938 692 587 775 3 out3
# 5 243 666 719 823 133 773 585 461 3 out3
library(purrr)
library(dplyr)
map(output, pluck, "parm", "stand") %>%
bind_rows(.id = "foo")
# foo V1 V2 V3 V4 V5 V6 V7 V8 grp
# 1 out1 845 527 296 902 358 447 317 347 1
# 2 out1 679 473 290 482 349 691 144 731 1
# 3 out1 842 574 135 894 628 542 757 174 1
# 4 out1 379 548 836 176 796 744 889 922 1
# 5 out1 498 837 492 965 255 508 138 689 1
# 6 out2 203 599 158 355 793 884 722 210 2
# 7 out2 543 693 484 195 511 174 793 654 2
# 8 out2 593 839 296 926 387 788 260 143 2
# 9 out2 373 363 323 939 416 348 792 211 2
# 10 out2 773 218 616 806 119 304 775 775 2
# 11 out3 171 217 859 899 664 737 114 837 3
# 12 out3 953 225 600 581 528 388 714 899 3
# 13 out3 615 550 860 134 667 136 987 993 3
# 14 out3 494 407 726 128 559 418 782 832 3
# 15 out3 729 734 432 354 716 288 734 264 3