Last active
June 2, 2017 21:08
-
-
Save johnmackintosh/217affee0f2b6fc8cfd71e15fc90f4a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## see http://johnmackintosh.com/2017-06-01-UPDATED-let-there-be-progress/ for background | |
#### direct from the expert - code kindly provided via blog comment by John Mount, Win-Vector LLC | |
library("dplyr") | |
library("wrapr") | |
d <- data.frame( | |
location = c('AreaZ', 'AreaZ', 'AreaZ', 'AreaW', 'AreaW', 'AreaW'), | |
date = structure(c(14761, 14791, 14822, 14853, 14883, 14914), class = "Date"), | |
y = c(6, 3, 8, 18, 18, 2), | |
stringsAsFactors = FALSE | |
) | |
wrapr_median_rows <- function(dframe, datecol, groupvar, colname, nrows) { | |
wrapr::let(c(COL= colname, | |
DATES= datecol, | |
GROUPVAR= groupvar), | |
{ | |
if(!is.null(groupvar)) { | |
dframe <- dframe %>% | |
dplyr::group_by(GROUPVAR) | |
} | |
dframe %>% | |
dplyr::arrange(DATES)%>% | |
dplyr::filter(row_number(DATES) %in% c(1:nrows)) %>% | |
dplyr::summarize(baseline = median(COL), | |
mindate = min(DATES), | |
maxdate = max(DATES)) | |
} | |
) | |
} | |
wrapr_median_rows(d,'date',NULL,'y',2) | |
## baseline mindate maxdate | |
## 1 4.5 2010-06-01 2010-07-01 | |
wrapr_median_rows(d,'date','location','y',2) | |
## # A tibble: 2 x 4 | |
## location baseline mindate maxdate | |
## <chr> <dbl> <date> <date> | |
## 1 AreaW 18.0 2010-09-01 2010-10-01 | |
## 2 AreaZ 4.5 2010-06-01 2010-07-01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment