Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cummean() translates to mean() not avg() #157

Closed
iangow opened this issue Sep 10, 2018 · 1 comment
Closed

cummean() translates to mean() not avg() #157

iangow opened this issue Sep 10, 2018 · 1 comment
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL wip work in progress

Comments

@iangow
Copy link

iangow commented Sep 10, 2018

library(dplyr, warn.conflicts = FALSE)
library(DBI)

df <- 
    expand.grid(id = LETTERS[1:6], time = 1:10) %>%
    arrange(id) %>%
    mutate(val = rnorm(n = n()))

df %>%
    group_by(id) %>%
    arrange(time) %>%
    mutate(cumavg = cummean(val)) %>%
    arrange(id, time)
#> # A tibble: 60 x 4
#> # Groups:   id [6]
#>    id     time    val cumavg
#>    <fct> <int>  <dbl>  <dbl>
#>  1 A         1  0.994 0.994 
#>  2 A         2  0.250 0.622 
#>  3 A         3  0.128 0.457 
#>  4 A         4  0.848 0.555 
#>  5 A         5  0.429 0.530 
#>  6 A         6 -2.09  0.0941
#>  7 A         7  0.709 0.182 
#>  8 A         8  2.13  0.426 
#>  9 A         9  0.893 0.478 
#> 10 A        10 -1.03  0.327 
#> # ... with 50 more rows

pg <- dbConnect(RPostgres::Postgres())

df_pg <- copy_to(pg, df)

df_pg %>%
    group_by(id) %>%
    arrange(time) %>%
    mutate(cumavg = cummean(val)) %>%
    arrange(id, time) %>%
    show_query()
#> <SQL>
#> SELECT "id", "time", "val", mean("val") OVER (PARTITION BY "id" ORDER BY "time" ROWS UNBOUNDED PRECEDING) AS "cumavg"
#> FROM (SELECT *
#> FROM "df"
#> ORDER BY "time") "qnxnacfsyq"
#> ORDER BY "id", "time"

Created on 2018-09-10 by the reprex package (v0.2.0).

@iangow
Copy link
Author

iangow commented Sep 11, 2018

Just in case it isn't clear from the above, mean is not valid SQL, but avg is.

@hadley hadley added bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL wip work in progress labels Jan 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior func trans 🌍 Translation of individual functions to SQL wip work in progress
Projects
None yet
Development

No branches or pull requests

2 participants