#' spreadx
#'
#' @inheritParams tidyr::spread
#' @param by @param .by a vector or list of variables as characters or given
#' by \code{vars}
#'
#' @export
#'
#' @examples
#' dat <- structure(list(
#' V1 = c("startTime", "max", "min", "EndTime", "avg",
#' "startTime", "max", "min", "EndTime", "avg"),
#' V2 = c(1L, 3L, 1L, 2L, 2L, 2L, 3L, 4L, 5L, 6L)),
#' .Names = c("V1", "V2"), class = "data.frame",
#' row.names = c(NA, -10L))
#' dat %>% spreadx(V1,V2, by = vars(cumsum(V1=="startTime")))
spreadx <- function(data, key, value, fill = NA, convert = FALSE, drop = TRUE,
sep = NULL, by = NULL){
# store names of by calls that were not created with an explicit name
# they will be removed in the ends
tmp_by_cols <- get_tmp_by_cols(data, by)
# if by is character, need to transform it
if (is.character(by))
by <- dplyr:::tbl_at_syms(data, by)
if (is.null(by))
# regular spread
x <- tidyr::spread(data, key, value, fill, convert, drop, sep)
else {
# record group state, group by additional new cols, spread, set groups back
groups <- dplyr::group_vars(data)
x <- dplyr::group_by(data,!!!by, add = TRUE)
x <- tidyr::spread(x, !!rlang::enquo(key), !!rlang::enquo(value),
fill, convert, drop, sep)
x <- dplyr::group_by(x, !!!rlang::syms(groups), add = FALSE)
# remove temporary by cols
if (length(tmp_by_cols)) x <- select(x,-!!sym(tmp_by_cols))
}
x
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.