首页 文章

从purrr获取映射以使用paste0

提问于
浏览
1

我有9个列表,其中包含对象分组$ ag . 我正试图从9个列表中的每个列表中提取该对象,并将这些列转换为tibble / df . 第一列应该具有组1:5 . 随后的9列每个都是一个带有5个数字的简单向量 . 对不起,这是不完整和可重复的..

map(hof2009_ag$groupings, "ag") %>% unlist 会正确地给我一个向量

[1] 789 615 525 425 352

但如果我动态地使用带有paste0的map,它就无法工作:
map(paste0("hof", i,"_ag$groupings"), "ag") .

所以,我试图得到9个名单, Headers 为hof2009:hof2017,使用迭代器通过for循环映射来访问每个 . 当我尝试使用paste0动态创建我的hof(i)_ag $分组时,它不再适用于map .

ag <- tibble(group=1:5)
for (i in 2009:2017) {
temp <- paste0("hof", i,"_ag$groupings") #works, "hof2009_ag$groupings"
TEST <- temp %>% map("ag") %>% unlist #fails, produces NULL
TEST <- hof2009_ag$groupings %>% map("ag") %>% unlist #works, produces 789 615 525 425 352
#ag <- map(temp, "ag") %>% unlist #doesn't work on "temp"
}

第二个问题是如何将这些添加为列 . 我玩过add_column和mutate以及ag [,i-2008],但是只要map不能用于粘贴,就无法使用它 .

如果我使用:

mget(paste0("hof", 2009:2009, "_ag")) %>%
map("groupings") %>% str

我明白了:

List of 1
 $ hof2009_ag:List of 5
 ..$ :List of 6
 .. ..$ prefs   :'data.frame':  14 obs. of  1 variable:
.. .. ..$ rank: int [1:14] 0 3 3 4 1 0 1 2 1 0 ...
.. ..$ ag      : int 789
.. ..$ grp     : int [1:60] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ iters   : num 1
.. ..$ run_time: Named num 1.13
.. .. ..- attr(*, "names")= chr "elapsed"
..$ :List of 6
.. ..$ prefs   :'data.frame':   14 obs. of  2 variables:
.. .. ..$ rank  : int [1:14] 0 3 4 5 2 1 2 3 1 1 ...
.. .. ..$ rank.1: int [1:14] 2 4 4 5 1 0 3 3 2 1 ...
.. ..$ ag      : int 615
.. ..$ grp     : int [1:60] 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ iters   : num 4
.. ..$ run_time: Named num 5.61
.. .. ..- attr(*, "names")= chr "elapsed"

编辑:使用dput的示例

> dput(hof2009_ag)

list(decision_makers = c(“Phil.Arvia”,“Steve.Aschburner”,“Filip.Bondy”,“Bob.Verdi”),alternative = c(“Harold.Baines”,“Bert.Blyleven”,“Alan . Trammell“),number_decision_makers = 60L,num_alts = 14L,groupings = list(list(prefs = structure(list(rank = c(0L,3L,3L,4L,1L,0L,1L)),class =”data.frame “,row.names = c(”Harold.Baines“,”Bert.Blyleven“,”Alan.Trammell“)),ag = 789L,grp = c(1L,1L,1L),iters = 1,run_time = c (elapsed = 1.12999999999738),grp2 =结构(列表(Decision_Maker = c(“Phil.Arvia”,“Steve.Aschburner”,“Filip.Bondy”,“Dave.Van.Dyck”,“Bob.Verdi”),Group_Number = c(“1”,“1”,“1”,“1”,“1”)),row.names = c(NA,-60L),class = c(“tbl_df”,“tbl”,“ data.frame“))),list(prefs = structure(list(rank = c(0L,3L,2L),rank.1 = c(2L,4L,2L)),class =”data.frame“,row .names = c(“Harold.Baines”,“Bert.Blyleven”,“Andre.Dawson”,“Alan.Trammell”),ag = 615L,grp = c(1L,1L,1L),iters = 4,run_time = c(elapsed = 5.61000000000058),grp2 =结构(列表(Decision_Maker = c (“Phil.Arvia”,“Steve.Aschburner”,“Steve.Wilmoth”,“Dave.Van.Dyck”,“Bob.Verdi”),Group_Number = c(“1”,“1”,“1”, “1”)),row.names = c(NA,-60L),class = c(“tbl_df”,“tbl”,“data.frame”))),list(prefs = structure(list(rank = c) (0L,3L,1L,1L),rank.1 = c(0L,4L,2L),rank.2 = c(1L,2L,2L))

1 回答

  • 1

    可重复的例子很有用 . 根据显示的代码,使用 mgetpaste 获取全局环境中的对象,然后使用 $[[ 循环 list 元素 select 'groupings' list 元素和'ag'(嵌套 list

    library(tidyverse)
    mget(paste0("hof", 2009:2017, "_ag")) %>%
            map(~ map(.x$groupings, ~ .x$ag))
    

相关问题