title: How to construct arguments for case_when from data frame?
body: <p>I'm trying to create many different possible weighting schemes based on temperature. </p>
  
  <p>I created a data frame with all possible combinations of 8 vectors (each vector represents a temperature range). So the columns of the data frame are a specific temperature range and the rows are weights.  </p>
  
  <p>I would like to pass the temperature ranges as arguments to <code>case_when</code>, and loop through each row of the weights data frame, creating a new variable for each row based on the actual temperature and the associated weight for that temperature based on the information in the weights data frame.</p>
  
  <p>Using the following post, I was able to create a function to produce the weights data frame:</p>
  
  <p><a href="https://stackoverflow.com/questions/44822256/use-dplyrcase-when-with-arguments-programmatically">Use dplyr::case_when with arguments programmatically</a></p>
  
  <p>But I don't know how to construct the <code>case_when</code> arguments using the weights data frame.</p>
  
  <h3>Function to create data frame of all possible weights</h3>
  
  <pre><code>library(rlang)
  library(tidyverse)
  
  create_temp_weights &lt;- function(
    from = 31,
    to = 100,
    by = 10,
    weights = exprs(between(., 31, 40) ~ c(0, 0.2),
                    between(., 41, 50) ~ c(0.5, 0.8),
                    between(., 51, 90) ~ c(0.8, 1),
                    between(., 91, 100) ~ c(0.2, 0.8),
                    TRUE ~ c(-0.1, 0))
  ) {
  
    # use 999 to map other temperatures to last case
    map(c(seq(from, to, by), 999), ~ case_when(!!!weights)) %&gt;%
      set_names(c(map_chr(seq(from, to, by),
                        ~ str_c("temp_", ., "_", . + by - 1)), "temp_other")) %&gt;%
    cross_df(.)
  
  }
  
  temp_weights &lt;- create_temp_weights()
  </code></pre>
  
  <h3>Create tibble with vector of temperatures used to construct the weights</h3>
  
  <pre><code>test_tibble &lt;- tibble(temp = seq_len(100))
  
  head(test_tibble)
  </code></pre>
  
  <h3>The following <code>case_when</code> is the thing I'm trying to produce programmatically using the weights data frame.</h3>
  
  <pre class="lang-r prettyprint-override"><code># Now I want to create a function that will produce the following
  # case_when from the temp_weight data frame so I don't have to
  # manually edit the following each time I create a new weights data frame
  
  test_tibble2 &lt;- map_dfc(.x = seq_len(nrow(temp_weights)),
      ~ transmute(
        test_tibble,
        temp =
          case_when(
            temp &gt;= 31   &amp; temp  &lt;= 40   ~  temp_weights$temp_31_40[.x],
            temp &gt;= 41   &amp; temp  &lt;= 50   ~  temp_weights$temp_41_50[.x],
            temp &gt;= 51   &amp; temp  &lt;= 60   ~  temp_weights$temp_51_60[.x],
            temp &gt;= 61   &amp; temp  &lt;= 70   ~  temp_weights$temp_61_70[.x],
            temp &gt;= 71   &amp; temp  &lt;= 80   ~  temp_weights$temp_71_80[.x],
            temp &gt;= 81   &amp; temp  &lt;= 90   ~  temp_weights$temp_81_90[.x],
            temp &gt;= 91   &amp; temp  &lt;= 100  ~  temp_weights$temp_91_100[.x],
            TRUE &amp; !is.na(temp)          ~  temp_weights$temp_other[.x]
          )
        ) %&gt;% set_names(paste0("temp_wt_", .x))
      ) 
  
  head(test_tibble2)
  
  </code></pre>
  
  <p>So what I'm looking for is a function that constructs the <code>case_when</code> arguments from a weights data frame.</p>