
set.seed(1234)ĭsmall I1 SI2 VVS2 VS1 VVS1 VS2 SI1 IF In those cases, summarize() generates one new row per value generated. Second, in some cases a function simply returns a vector as output. #> # … with 2 more variables: kurtosis, se #> cut vars n mean sd median trimmed mad min max range skew #> `summarise()` ungrouping output (override with `.groups` argument) #> vars n mean sd median trimmed mad min max range skew


If we use such a function in summarize(), it generates a data-frame column, which we can turn into separate columns via unpack().

In recent versions of the tidyverse, this is possible.įirst, in the example you provided, the function returns a one-row data frame. We can write own summary function which returns a list: fun cut n min median mean sd max #> cut vars n mean sd median trimmed mad min max range skew kurtosis se Solution based on the purrr ( purrrlyr since 2017) package: library(ggplot2)īy_slice(~ describe(.x$price). #> cut n mean sd median trimmed mad min max range skew kurtosis se I'm using dbplyr, so I have to do it in a way that it can get translated into SQL.With dplyr >= 0.2 we can use do function for this: library(ggplot2) However, the conditions are independent, so I'd like to add the summary variables independently as well. summarize_num % summarize(n=n(), n_unique=n_distinct())ĭepending on conditions (summarize_num, and summarize_num_distinct here), the eventual summary (summ here) has different columns.Īs the number of conditions goes up, the number of clauses goes up combinatorially.
Dplyr summarize sum values code#
I see several related stackoverflow questions, but I didn't see this.ĮDIT: Here is some precise example code, but simplified from the real code (which has more than two conditionals). Is there an analogous way to add summary variables to a summarize statement? I have some complicated conditionals (with dbplyr) where if x=TRUE I want to add For example: data % group_by(a, add=TRUE)

In dplyr, group_by has a parameter add, and if it's true, it adds to the group_by.
