Why is using list() critical for .dots = setNames() uses in dplyr?

March 22, 2016
By

[This article was first published on Higher Order Functions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I wrote an answer
about why setNames() shows up sometimes in standard evaluation with dplyr.

My explanation turned into a mini-tutorial on why those standard evaluation
functions have a .dots argument. The basic idea is that the usual variadic
argument ... is a series of expressions that get evaluated inside of the
dataframe.

library("dplyr")

# standardize and round
z_round <- . %>% scale %>% as.numeric %>% round(2)

# The two expressions defining zSL, zSW are the `...`
iris %>% 
  mutate_(zSL = ~ z_round(Sepal.Length), 
          zSW = ~ z_round(Sepal.Width)) %>%
  tbl_df
#> # A tibble: 150 × 7
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   zSL   zSW
#>                                     
#> 1           5.1         3.5          1.4         0.2  setosa -0.90  1.02
#> 2           4.9         3.0          1.4         0.2  setosa -1.14 -0.13
#> 3           4.7         3.2          1.3         0.2  setosa -1.38  0.33
#> 4           4.6         3.1          1.5         0.2  setosa -1.50  0.10
#> 5           5.0         3.6          1.4         0.2  setosa -1.02  1.25
#> 6           5.4         3.9          1.7         0.4  setosa -0.54  1.93
#> 7           4.6         3.4          1.4         0.3  setosa -1.50  0.79
#> 8           5.0         3.4          1.5         0.2  setosa -1.02  0.79
#> 9           4.4         2.9          1.4         0.2  setosa -1.74 -0.36
#> 10          4.9         3.1          1.5         0.1  setosa -1.14  0.10
#> # ... with 140 more rows

If we programmatically assemble or manipulate those expressions before calling
mutate_, we can’t use that ..., because we have a list of expressions, not
a series of individual expressions. We use the .dots argument instead.

exps <- list(
  zSL = ~ z_round(Sepal.Length), 
  zSW = ~ z_round(Sepal.Width)
)

iris %>% mutate_(exps)
#> Error in UseMethod("as.lazy"): no applicable method for 'as.lazy' applied to an object of class "list"

iris %>% mutate_(.dots = exps) %>% tbl_df
#> # A tibble: 150 × 7
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   zSL   zSW
#>                                     
#> 1           5.1         3.5          1.4         0.2  setosa -0.90  1.02
#> 2           4.9         3.0          1.4         0.2  setosa -1.14 -0.13
#> 3           4.7         3.2          1.3         0.2  setosa -1.38  0.33
#> 4           4.6         3.1          1.5         0.2  setosa -1.50  0.10
#> 5           5.0         3.6          1.4         0.2  setosa -1.02  1.25
#> 6           5.4         3.9          1.7         0.4  setosa -0.54  1.93
#> 7           4.6         3.4          1.4         0.3  setosa -1.50  0.79
#> 8           5.0         3.4          1.5         0.2  setosa -1.02  0.79
#> 9           4.4         2.9          1.4         0.2  setosa -1.74 -0.36
#> 10          4.9         3.1          1.5         0.1  setosa -1.14  0.10
#> # ... with 140 more rows

To leave a comment for the author, please follow the link and comment on their blog: Higher Order Functions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)