-
Notifications
You must be signed in to change notification settings - Fork 3
/
mutate.R
45 lines (37 loc) · 1008 Bytes
/
mutate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#' @title Add new variables
#' @description Mimics dplyr::mutate using base R and rlang
#' @param .data data.frame
#' @param ... Name-value pairs of expressions
#' @return data.frame
#' @examples
#' airquality%>%
#' b_mutate(
#' lOzone = log(Ozone),
#' Month = factor(month.abb[Month]),
#' cTemp = round((Temp - 32) * 5/9, 1),
#' S.cT = Solar.R / cTemp
#' )%>%
#' head()
#' @seealso
#' \code{\link[rlang]{quotation}}
#' @rdname b_mutate
#' @export
#' @author Jonathan Sidi
b_mutate <- function(.data,...){
UseMethod("b_mutate")
}
#' @rdname b_mutate
#' @export
#' @importFrom rlang quos quo_squash
b_mutate.default <- function(.data,...){
FNS <- lapply(rlang::quos(...),rlang::quo_squash)
EXPRS <- lapply(names(FNS),function(x){
sprintf('%s <- %s',x,deparse(FNS[[x]]))
})
within(.data,eval(parse(text = paste0(unlist(EXPRS),collapse = '\n'))))
}
#' @rdname b_mutate
#' @export
b_mutate.split_df <- function(.data,...){
bindr(split(.data), b_mutate, .data, ...)
}