title: How to find the mean of multiple columns based on a second dataset?
body: <h2>Problem</h2>
  <p>I need to use a dictionary dataset to determine which columns from a different dataset I should calculate the mean.</p>
  <h2>Data</h2>
  <p>I will illustrate my case with the <code>iris</code> dataset (a dataset already in R).</p>
  <p>I have two datasets:</p>
  <ol>
  <li>The actual data - like <code>iris</code> (however the col names is basically a1, a2, a3, a4...).</li>
  <li>The dictionary for the former indicates what each <code>iris</code> column indicates. The column <code>feature</code> in the <code>dictionary_iris</code> is the grouping variable, and it would also be part of the name for the new variables (e.g., the new variable would be called <code>Sepal_mean</code>, or <code>Petal_mean</code>).</li>
  </ol>
  <h3>Iris dataset</h3>
  <pre><code>library(dplyr)
  
  iris %&gt;% as_tibble()
  
    # # A tibble: 150 x 5
    # Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    # &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;  
    # 1          5.1         3.5          1.4         0.2 setosa 
    # 2          4.9         3            1.4         0.2 setosa 
    # 3          4.7         3.2          1.3         0.2 setosa 
    # 4          4.6         3.1          1.5         0.2 setosa 
    # 5          5           3.6          1.4         0.2 setosa 
    # 6          5.4         3.9          1.7         0.4 setosa 
    # 7          4.6         3.4          1.4         0.3 setosa 
    # 8          5           3.4          1.5         0.2 setosa 
    # 9          4.4         2.9          1.4         0.2 setosa 
    # 10          4.9         3.1          1.5         0.1 setosa 
    # # ... with 140 more rows
  </code></pre>
  <h2>Dictionary Dataset</h2>
  <pre><code>dictionary_iris &lt;- tibble(variables = names(iris)) %&gt;% 
    separate(variables, into = c(&quot;feature&quot;, &quot;measure&quot;), remove = FALSE)
  
  dictionary_iris
  
  # # A tibble: 5 x 3
  # variables    feature measure
  # &lt;chr&gt;        &lt;chr&gt;   &lt;chr&gt;  
  # 1 Sepal.Length Sepal   Length 
  # 2 Sepal.Width  Sepal   Width  
  # 3 Petal.Length Petal   Length 
  # 4 Petal.Width  Petal   Width  
  # 5 Species      Species NA 
  </code></pre>
  <h2>Expected Output</h2>
  <p>I know how to do that manually (see below), but I would like to automatize this process because I have a dataframe with more than 300 columns and want to take 23 different means across those columns.</p>
  <pre><code>library(dplyr)
  
  iris %&gt;% 
    rowwise() %&gt;% 
    mutate(Sepal_mean = mean(c(Sepal.Length, Sepal.Width), na.rm = TRUE),
           Petal_mean = mean(c(Petal.Length, Petal.Width), na.rm = TRUE))
  
  
  # # A tibble: 150 x 7
  # # Rowwise: 
  # Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal_mean Petal_mean
  # &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;        &lt;dbl&gt;      &lt;dbl&gt;
  # 1          5.1         3.5          1.4         0.2 setosa        4.3        0.8 
  # 2          4.9         3            1.4         0.2 setosa        3.95       0.8 
  # 3          4.7         3.2          1.3         0.2 setosa        3.95       0.75
  # 4          4.6         3.1          1.5         0.2 setosa        3.85       0.85
  # 5          5           3.6          1.4         0.2 setosa        4.3        0.8 
  # 6          5.4         3.9          1.7         0.4 setosa        4.65       1.05
  # 7          4.6         3.4          1.4         0.3 setosa        4          0.85
  # 8          5           3.4          1.5         0.2 setosa        4.2        0.85
  # 9          4.4         2.9          1.4         0.2 setosa        3.65       0.8 
  # 10          4.9         3.1          1.5         0.1 setosa        4          0.8 
  # # ... with 140 more rows
  </code></pre>
  <p>I have the impression that I can do that with <code>dplyr::mutate()</code> and <code>dplyr::across()</code> or with some <code>dplyr::map()</code> function. But I got quite lost.</p>