This may be an issue with dplyr as it seems like it evaluates the .%>% ...
sequence to something else:
library(dplyr)
iris %>%
left_join({
print(. %>%
mutate(new = 5L))
},
copy = TRUE)
#> Functional sequence with the following components:
#>
#> 1. mutate(., new = 5L)
#>
#> Use 'functions' to extract the individual functions.
#> Error in as.data.frame.default(y): cannot coerce class 'c("fseq", "function")' to a data.frame
You may want to report the issue to Github or I can follow-up with it on Github - not sure if this is more magrittr or dplyr. I used to use similar pipes. As @duck notes, it appears wrapping {.}
around the dot escapes whatever is happening.
iris %>%
{
{.}%>%
group_by(Species)%>%
summarize(N = n())
}
Also... the inevitable data.table
alternative:
library(data.table)
dt = as.data.table(iris)
dt[, N := .N, by = Species]
dt
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species N
#> 1: 5.1 3.5 1.4 0.2 setosa 50
#> 2: 4.9 3.0 1.4 0.2 setosa 50
#> 3: 4.7 3.2 1.3 0.2 setosa 50
#> 4: 4.6 3.1 1.5 0.2 setosa 50
#> 5: 5.0 3.6 1.4 0.2 setosa 50
#> ---
#> 146: 6.7 3.0 5.2 2.3 virginica 50
#> 147: 6.3 2.5 5.0 1.9 virginica 50
#> 148: 6.5 3.0 5.2 2.0 virginica 50
#> 149: 6.2 3.4 5.4 2.3 virginica 50
#> 150: 5.9 3.0 5.1 1.8 virginica 50