You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, contrary to most languages, in R **symbols can be treated as objects themselves**. This allows dplyr to even perform such simplifications. The details are irrelevant now
139
+
Elegant, isn't it? Now, let's proceed by encapsulating this logic within a function where column names are passed as strings:
# do_magic(iris, special = "Petal.Length", others = c("Sepal.Length", "Sepal.Width"))
149
+
```
150
+
151
+
Surprisingly, it fails! When used within the context of `across`, dplyr seems unable to utilize the tidyselect rules (the ones that make `all_of()` possible). But we're not defeated; let's try embracing:
do_magic_but_better(iris, special = Petal.Length, others = c("Sepal.Length", "Sepal.Width"))
160
+
```
161
+
162
+
By adopting this approach, it's imperative to provide special as a symbol. Also, this does not look fine: one parameter is provided as symbol, another one is as character vector... **We should always aim at being consistent**. Either all column-like parameters should be symbols or all should be character strings. There are pros and cons to both ways. Let's say that we want to stick to strings only. How can we do it?
163
+
164
+
#### Tip: when `all_of()` does not work, use `.data`
do_magic_but_in_other_way(iris, special = "Petal.Length", others = c("Sepal.Length", "Sepal.Width"))
175
+
```
176
+
177
+
When you need to reference the underlying data within the context of functions, the `.data` pronoun comes to the rescue. As demonstrated, it operates similarly to directly accessing the data.
178
+
179
+
## Summary & Next Steps
180
+
181
+
Throughout this post, we ventured deep into some of the intricacies of dplyr. We've unraveled how the package strives to make our code both semantic and syntactic, all while simplifying complex operations. The power of symbols and the utility of functions like `all_of()` and `.data` demonstrate just how dynamic and adaptable dplyr can be, especially when working with variable column names. While we've covered much ground, the world of dplyr is vast and constantly evolving. We are aware that all this *embracing* and *tidyselect* rules might be intimidating, but we will be continue to explore more facets of the tidyverse in future posts of "basic advanceds", aiming to empower you with advanced techniques that enhance your data analysis journey.
182
+
183
+
If you've found this post enlightening and wish to delve deeper, or if you have any questions or insights, we'd love to hear from you! You can contact us directly via [X](https://twitter.com/Rturtletopia). Alternatively, for those who prefer a more open-source avenue, feel free to open an issue on our [GitHub](https://github.com/turtletopia/turtletopia.github.io/issues) repository. Your feedback and insights not only help us improve, but they also contribute to the broader data science community.
184
+
185
+
Until next time, keep exploring, learning, and sharing!
186
+
187
+
## Dive Deeper: Resources for the Curious Minds:
188
+
189
+
For those wishing to delve further or who may have lingering questions: [Dplyr official programming guide](https://dplyr.tidyverse.org/articles/programming.html)
Copy file name to clipboardExpand all lines: content/post/2023-01-30-r-basic-advanceds-variables-and-names-in-dplyr/index.html
+49-1Lines changed: 49 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -112,5 +112,53 @@ <h1>Problem 3: dynamic columns in purrr formulas in <code>across</code></h1>
112
112
<p>For custom, unnamed functions, the <em>purrr formula syntax</em> (<code>~ expression</code> with <code>.x</code>) is beneficial. In our case (without enclosing it in a function yet) could look like:</p>
<p>However, contrary to most languages, in R <strong>symbols can be treated as objects themselves</strong>. This allows dplyr to even perform such simplifications. The details are irrelevant now</p>
115
+
<p>Elegant, isn’t it? Now, let’s proceed by encapsulating this logic within a function where column names are passed as strings:</p>
# do_magic(iris, special = "Petal.Length", others = c("Sepal.Length", "Sepal.Width"))</code></pre>
123
+
<p>Surprisingly, it fails! When used within the context of <code>across</code>, dplyr seems unable to utilize the tidyselect rules (the ones that make <code>all_of()</code> possible). But we’re not defeated; let’s try embracing:</p>
do_magic_but_better(iris, special = Petal.Length, others = c("Sepal.Length", "Sepal.Width"))</code></pre>
130
+
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
131
+
## 1 3.7 2.1 1.4 0.2 setosa
132
+
## 2 3.5 1.6 1.4 0.2 setosa
133
+
## 3 3.4 1.9 1.3 0.2 setosa
134
+
## 4 3.1 1.6 1.5 0.2 setosa
135
+
## 5 3.6 2.2 1.4 0.2 setosa</code></pre>
136
+
<p>By adopting this approach, it’s imperative to provide special as a symbol. Also, this does not look fine: one parameter is provided as symbol, another one is as character vector… <strong>We should always aim at being consistent</strong>. Either all column-like parameters should be symbols or all should be character strings. There are pros and cons to both ways. Let’s say that we want to stick to strings only. How can we do it?</p>
do_magic_but_in_other_way(iris, special = "Petal.Length", others = c("Sepal.Length", "Sepal.Width"))</code></pre>
146
+
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
147
+
## 1 3.7 2.1 1.4 0.2 setosa
148
+
## 2 3.5 1.6 1.4 0.2 setosa
149
+
## 3 3.4 1.9 1.3 0.2 setosa
150
+
## 4 3.1 1.6 1.5 0.2 setosa
151
+
## 5 3.6 2.2 1.4 0.2 setosa</code></pre>
152
+
<p>When you need to reference the underlying data within the context of functions, the <code>.data</code> pronoun comes to the rescue. As demonstrated, it operates similarly to directly accessing the data.</p>
<p>Throughout this post, we ventured deep into some of the intricacies of dplyr. We’ve unraveled how the package strives to make our code both semantic and syntactic, all while simplifying complex operations. The power of symbols and the utility of functions like <code>all_of()</code> and <code>.data</code> demonstrate just how dynamic and adaptable dplyr can be, especially when working with variable column names. While we’ve covered much ground, the world of dplyr is vast and constantly evolving. We are aware that all this <em>embracing</em> and <em>tidyselect</em> rules might be intimidating, but we will be continue to explore more facets of the tidyverse in future posts of “basic advanceds”, aiming to empower you with advanced techniques that enhance your data analysis journey.</p>
157
+
<p>If you’ve found this post enlightening and wish to delve deeper, or if you have any questions or insights, we’d love to hear from you! You can contact us directly via <ahref="https://twitter.com/Rturtletopia">X</a>. Alternatively, for those who prefer a more open-source avenue, feel free to open an issue on our <ahref="https://github.com/turtletopia/turtletopia.github.io/issues">GitHub</a> repository. Your feedback and insights not only help us improve, but they also contribute to the broader data science community.</p>
158
+
<p>Until next time, keep exploring, learning, and sharing!</p>
<h2>Dive Deeper: Resources for the Curious Minds:</h2>
162
+
<p>For those wishing to delve further or who may have lingering questions: <ahref="https://dplyr.tidyverse.org/articles/programming.html">Dplyr official programming guide</a></p>
0 commit comments