title: Mutate across multiple variables, using a list of third variables in R
body: <p>I need to obtain values of a list of grades (math, language, sci, etc.) conditional on the presence of valid values in 2016 (<code>validity_2016</code>==&quot;yes&quot;), into a new variable called <code>grades_{subjects}</code> (eg. grades_math).</p>
  <pre><code>df&lt;-tibble(person = c(&quot;Alice&quot;, &quot;Bob&quot;, &quot;Mary&quot;),
             validity_2016 = c(NA, &quot;yes&quot;, NA),
             likes_ham = c(&quot;no&quot;, &quot;yes&quot;, &quot;yes&quot;),
             grades_math_2015=c(6,2,4),
             grades_math_2016=c(3,5,7),
             grades_language_2015=c(7,1,9),
             grades_language_2016=c(3,6,7),
             grades_sci_2015=c(7,1,9),
             grades_sci_2016=c(3,6,7))
  </code></pre>
  <p>I was wondering the viability to use <code>dplyr</code>'s <code>mutate_at</code> or <code>mutate(across</code>, in the following way:</p>
  <pre><code>dplyr::mutate(across(grades_math_2016, grades_language_2016,grades_sci_2016),
  ~dplyr::case_when(!is.na(validity_2016)~list(grades_math_2015,grades_language_2015,grades_sci_2015)~.),
  .names=&quot;{col}&quot;))
  </code></pre>
  <p>The outcomes should look like this:</p>
  <pre><code>df&lt;-tibble(person = c(&quot;Alice&quot;, &quot;Bob&quot;, &quot;Mary&quot;),
                 validity_2016 = c(NA, &quot;yes&quot;, NA),
                 likes_ham = c(&quot;no&quot;, &quot;yes&quot;, &quot;yes&quot;),
                 grades_math_2015=c(6,2,4),
                 grades_math_2016=c(3,5,7),
                 grades_language_2015=c(7,1,9),
                 grades_language_2016=c(3,6,7),
                 grades_sci_2015=c(7,1,9),
                 grades_sci_2016=c(3,6,7),
                 grades_math=c(6,5,4),
                 grades_language=c(7,6,7),
                 grades_sci=c(7,6,9))
  </code></pre>