简体   繁体   English

使用 purrr::map_dfr 从值而不是名称中分配 id 值

[英]assigning id values from values, not names, with purrr::map_dfr

I think this question is related to Using map_dfr and.id for list names and list of list names but not identical...我认为这个问题与使用 map_dfr and.id for list names 和 list of list names但不完全相同...

I often use map_dfr for a case where I want to use the value of each argument, not its name , as the .id variable.我经常将map_dfr用于我想使用每个参数的而不是它的name作为.id变量的情况。 Here's a silly example: I am computing the mean of mtcars$mpg raised to the second, fourth, and sixth power:这是一个愚蠢的例子:我正在计算mtcars$mpg的二、四和六次方的平均值:

library(tidyverse)
list(2,4,6) %>% map_dfr(~tibble(x=mean(mtcars$mpg^.)), .id="name")
##   name           x
##   <chr>      <dbl>
## 1 1           439.
## 2 2        262350.
## 3 3     198039783.

I would like the name variable to be 2, 4, 6 instead of 1, 2, 3. I can hack this by including setNames(.data) in the pipeline:我希望name变量为 2、4、6 而不是 1、2、3。我可以通过在管道中包含setNames(.data)来破解它:

list(2,4,6) %>% 
   setNames(.data) %>% 
   map_dfr(~tibble(x=mean(mtcars$mpg^.)), .id="name")

but I wonder if there is a more idiomatic approach I'm missing?但我想知道我是否缺少更惯用的方法?


As for the suggestion of using something like ~ tible(name=., ...) : nice, but slightly less convenient for the case where the mapping function already returns a tibble, because we have to add an otherwise unnecessary tibble() call:至于使用类似~ tible(name=., ...)的建议:很好,但对于映射function已经返回一个 tibble 的情况来说不太方便,因为我们必须添加一个否则不必要的tibble()调用:

list(2, 4, 6) %>%   
    map_dfr(~ tibble(name=., 
       broom::tidy(lm(mpg~cyl, data=mtcars, offset=rep(., nrow(mtcars))))))

OK, I think I found this shortly before posting (so I'll answer).好的,我想我在发布前不久发现了这个(所以我会回答)。 This answer points out that tibble::lst() is a self-naming list function, so as long as we use tibble::lst(2,4,6) instead of list(2,4,6) , it Just Works, eg 这个答案指出tibble::lst()是一个自命名列表 function,所以只要我们使用tibble::lst(2,4,6)而不是list(2,4,6) ,它就可以工作,例如

lst(2,4,6) %>% map_dfr(~tibble(x=mean(mtcars$mpg^.)), .id="name")

This can work too:这也可以工作:

library(tidyverse)

#@ben Bolker answer.
lst(2,4,6) %>% map_dfr(~tibble(x=mean(mtcars$mpg^.)), .id="power")
#> # A tibble: 3 x 2
#>   power          x
#>   <chr>      <dbl>
#> 1 2           439.
#> 2 4        262350.
#> 3 6     198039783.

list(2, 4, 6) %>% map_df(~ tibble(power = as.character(.x) , x = mean(mtcars$mpg^.)))
#> # A tibble: 3 x 2
#>   power          x
#>   <chr>      <dbl>
#> 1 2           439.
#> 2 4        262350.
#> 3 6     198039783.

#another option
seq(2, 6, 2) %>%  map2_df(rerun(length(.), mtcars$mpg), ~ c(x = as.character(.x), mean = round(mean(.y^.x), 0)))
#> # A tibble: 3 x 2
#>   x     mean     
#>   <chr> <chr>    
#> 1 2     439      
#> 2 4     262350   
#> 3 6     198039783

Created on 2021-06-06 by the reprex package (v2.0.0)代表 package (v2.0.0) 于 2021 年 6 月 6 日创建

This is also possible, however it would not have been my first choice and only a map would suffice:这也是可能的,但它不是我的首选,只有map就足够了:

library(purrr)

list(2, 4, 6) %>%
  pmap_dfr(~ tibble(power = c(...), x = map_dbl(c(...), ~ mean(mtcars$mpg ^ .x))))

# A tibble: 3 x 2
  power          x
  <dbl>      <dbl>
1     2       439.
2     4    262350.
3     6 198039783.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM