library("magrittr")
# example 1 (simple case with dataframe)
# Using iris dataset,
# compute cumulative mean of column `Sepal.Length`
# ordered by `Petal.Width` and `Sepal.Width` columns
# grouped by `Petal.Length` column
iris %>%
tidier::mutate_(sl_mean = mean(Sepal.Length),
.order_by = c("Petal.Width", "Sepal.Width"),
.by = "Petal.Length",
.frame = c(Inf, 0),
) %>%
dplyr::slice_min(n = 3, Petal.Width, by = Species)
# example 2 (detailed case with dataframe)
# Using a sample airquality dataset,
# compute mean temp over last seven days in the same month for every row
set.seed(101)
airquality %>%
# create date column
dplyr::mutate(date_col = lubridate::make_date(1973, Month, Day)) %>%
# create gaps by removing some days
dplyr::slice_sample(prop = 0.8) %>%
dplyr::arrange(date_col) %>%
# compute mean temperature over last seven days in the same month
tidier::mutate_(avg_temp_over_last_week = mean(Temp, na.rm = TRUE),
.order_by = "Day",
.by = "Month",
.frame = c(lubridate::days(7), # 7 days before current row
lubridate::days(-1) # do not include current row
),
.index = "date_col"
)
# example 3
airquality %>%
# create date column as character
dplyr::mutate(date_col =
as.character(lubridate::make_date(1973, Month, Day))
) %>%
tibble::as_tibble() %>%
# as `tbl_lazy`
dbplyr::memdb_frame() %>%
mutate_(avg_temp = mean(Temp),
.by = "Month",
.order_by = "date_col",
.frame = c(3, 3)
) %>%
dplyr::collect() %>%
dplyr::select(Ozone, Solar.R, Wind, Temp, Month, Day, date_col, avg_temp)
Run the code above in your browser using DataLab