如何将第三个动态列表元素添加到我的 pmap_dfc 语句中 [英] How do I add a third dynamic list element to my pmap_dfc statement

查看:33
本文介绍了如何将第三个动态列表元素添加到我的 pmap_dfc 语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下工作代码,它利用两个列表来生成模拟输出:

I have the following working code that utilizes two lists to produce simulation output:

strategy_list <- list("s_Win","s_WinH1", "s_WinH2", "s_WinH1F1", "s_WinH2F2", "s_WinDerEx")
function_list <- list(s_win, s_winH1, s_winH2, s_winH1F1, s_winH2F2, s_winDerEx)
l <- list(strategy_list, function_list)
simulation <- pmap_dfc(l, ~ df %>%
                      transmute(!! .x := .y(entries, skill, field, win_payoff, wager_amt, Winner, exacta_payoff))) %>%
  bind_cols(df, .) 

现在我想在几个不同的技能水平上运行模拟,所以我添加了一个循环并尝试用循环中的 i 替换技能输入以创建模拟的几种变体:

Now I would like to run the simulation at several different skill levels, so I have added a loop and have tried to replace the skill input with i from the loop to create several variations of the simulation:

for (i in seq(from = 0.15, to=0.30, by=0.05)){
skill_list <- list(i, i, i, i, i, i)
strategy_list <- list("s_Win","s_WinH1", "s_WinH2", "s_WinH1F1", "s_WinH2F2", "s_WinDerEx")
function_list <- list(s_win, s_winH1, s_winH2, s_winH1F1, s_winH2F2, s_winDerEx)
l <- list(skill_list, strategy_list, function_list)
simulation <- pmap_dfc(l, ~ df %>%
                      transmute(!! .w !! .x := .y(entries, i, field, win_payoff, wager_amt, Winner, exacta_payoff))) %>%
  bind_cols(df, .)
}

不幸的是,这会产生错误.我尝试了几种变体,但似乎无法使代码正常工作.

Unfortunately, this is producing an error. I have tried several variations, but can't seem to get the code to work.

根据下面 Atem 的帖子,我更新了我的代码如下:

Based upon Atem's post below, I have updated my code as follows:

for (i in seq(from = 0.15, to=0.30, by=0.05)){
strategy_list <- list("s_Win","s_WinH1", "s_WinH2", "s_WinH1F1", "s_WinH2F2", "s_WinDerEx") %>% stringr::str_c(i)
function_list <- list(s_win, s_winH1, s_winH2, s_winH1F1, s_winH2F2, s_winDerEx)
skill_list <- list(i, i, i, i, i, i)
l <- list(strategy_list, function_list, skill_list)
simulation <- pmap_dfc(l, ~ df %>%
                      transmute(!! ..1 := ..2 (entries, ..3, field, win_payoff, wager_amt, Winner, exacta_payoff))) %>%
  bind_cols(df, .)  %>% 

不幸的是,这仍然会产生错误.问题似乎出在 ..2 上,因为它没有收到与 ..1 和 ..3 相同的语法突出显示.

Unfortunately, this is still producing an error. The problem appears to be with ..2 as this does not receive the same syntax highlighting as ..1 and ..3.

编辑 2:为了使这更简单,我整理了我的问题的简化版本并包含了一个reprex.带有两个列表的 Simulation1 工作正常.Simulation2 包含三个列表,循环失败并显示错误消息:找不到函数..2".

EDIT 2: To make this a bit simpler I have put together a simplified version of my question and included a reprex. Simulation1 with two lists works fine. Simulation2 with three lists and the loop fails with the error message: could not find function "..2".

``` r
library(tidyverse)
z <- 5

df <- tibble(x=1:10, y=1:10)

s_win <- function(x,y,z){
a <-rnorm(x) + x + y + 1 +z
a
}

s_win1 <- function(x,y,z){
b <-  rnorm(x) + x + y + 2 + z
b
}

s_win2 <- function(x,y,z){
c <-  rnorm(x) + x + y + 3 +z
c
}

# Simulation1 with two list works.  

strategy_list <- list("s_Win","s_Win1", "s_Win2") 
function_list <- list(s_win, s_win1, s_win2)
l <- list(strategy_list, function_list)
simulation1 <- pmap_dfc(l, ~ df %>%
                      transmute(!! .x := .y (x, y, z))) %>%
  bind_cols(df, .)  %>% 
  pivot_longer(
   cols = starts_with("s_"),
   names_to = "Strategy",
   names_prefix = "s_",
   values_to = "Value",
   values_drop_na = TRUE
 ) 
    
View(simulation1)


# Simulation 2 with thre list does not work.  Error message = could not find function "..2"

for (i in seq(from = 5, to=20, by=5)){
strategy_list <- list("s_Win","s_Win1", "s_Win2") %>% stringr::str_c(i)
function_list <- list(s_win, s_win1, s_win2)
skill_list <- list(i, i, i)
l <- list(strategy_list, function_list, skill_list)
simulation2 <- pmap_dfc(l, ~ df %>%
                      transmute(!! ..1 := ..2 (x, y, ..3))) %>%
  bind_cols(df, .)  %>% 
  pivot_longer(
   cols = starts_with("s_"),
   names_to = "Strategy",
   names_prefix = "s_",
   values_to = "Value",
   values_drop_na = TRUE
 )
}
#> Error: Problem with `mutate()` input `s_Win5`.
#> x could not find function "..2"
#> i Input `s_Win5` is `..2(x, y, ..3)`.
View(simulation2)  
#> Error in as.data.frame(x): object 'simulation2' not found
```

reprex 包 (v0.3.0) 于 2020 年 11 月 25 日创建

Created on 2020-11-25 by the reprex package (v0.3.0)

推荐答案

列名存储在 strategy_list 中,您需要在其中合并 i:

The column names are stored in strategy_list, which is where you will want to incorporate i:

strategy_list <- list("s_Win","s_WinH1", "s_WinH2",
                      "s_WinH1F1", "s_WinH2F2", "s_WinDerEx") %>% 
                   stringr::str_c(i)

因为您现在在 l 中有三个列表,您还需要切换到使用 ..1..2 等. 而不是 .x.y(它们只适用于两组参数):

Because you now have three lists in l, you will also want to switch to using ..1, ..2, etc. instead of .x and .y (which are only appropriate for two sets of arguments):

simulation <- pmap_dfc(l, ~ df %>%
                      transmute(!! ..1 := rlang::exec(..2, entries, ..3, field, win_payoff, 
                                              wager_amt, Winner, exacta_payoff))) %>%
  bind_cols(df, .)

小注:!! 运算符称为取消引用".如果没有它,transmute 将创建一个名为 .x 的列,而不是使用存储在 .x 中的名称.下面是一个演示差异的示例:

Minor note: The !! operator is known as "unquoting". Without it, transmute would create a column called .x instead of using the names stored in .x. Here's an example that demonstrates the difference:

x <- "result"
mtcars %>% transmute( x = "Hello World" )
#              x
# 1  Hello World
# 2  Hello World
# ...

mtcars %>% transmute( !!x := "Hello World" )
#         result
# 1  Hello World
# 2  Hello World
# ...

编辑以解决 ..2 问题:出于某种原因,pmap 在解释 ..2 时存在问题代码> 包含一个函数.一个简单的解决方法是使用 rlang::exec 使用给定的参数执行该函数:

EDIT to address the ..2 issue: For some reason, pmap has issues with interpreting ..2 as containing a function. A simple workaround is to use rlang::exec to execute that function with the given arguments:

simulation2 <- pmap_dfc(l, ~ df %>%
                      transmute(!! ..1 := rlang::exec(..2, x, y, ..3))) %>%
    # ... as before

我也更新了上面的原始答案.

I updated the original answer above as well.

这篇关于如何将第三个动态列表元素添加到我的 pmap_dfc 语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆