2019-09-20
It is prevalent
$
and named element parameterslm()
and glm()
subset
parameterIt is prevalent
$
and named element parameterslm()
and glm()
subset
parameterWe have nice utilities for dealing with symbols and expressions
substitute
, quote
, enquote
, bquote
codetools
packageA dialect takes a subset of all R expressions
A dialect takes a subset of all R expressions
This could be taken a lot further with a parsing package
lme4
by Doug Bates%>%
by Stefan Milton Bache and Hadley Wickhamdata.table
[
and [<-
infix operators by Matt Dowlearmacmp
by Dirk Schumachersurvival
(mult-state models) by Terry Therneauiris[order(iris[,"Sepal.Width"] )[1:3], "Sepal.Width", drop = FALSE]
## Sepal.Width ## 61 2.0 ## 63 2.2 ## 69 2.2
library(dplyr) iris %>% select(Sepal.Width) %>% arrange(Sepal.Width) %>% head(3)
## Sepal.Width ## 1 2.0 ## 2 2.2 ## 3 2.2
magrittr
doing?foo <- . %>% head %>% tail(n=5) foo
## Functional sequence with the following components: ## ## 1. head(.) ## 2. tail(., n = 5) ## ## Use 'functions' to extract the individual functions.
unclass(foo)
## function (value) ## freduce(value, `_function_list`) ## <environment: 0x7fd9025b3d18>
magrittr
doing?ls(environment(foo))
## [1] "_fseq" "_function_list" "freduce"
magrittr
doing?environment(foo)$`_function_list`
## [[1]] ## function (.) ## head(.) ## ## [[2]] ## function (.) ## tail(., n = 5)
magrittr
doing?environment(foo)$`freduce`
## function (value, function_list) ## { ## k <- length(function_list) ## if (k > 1) { ## for (i in 1:(k - 1L)) { ## value <- function_list[[i]](value) ## } ## } ## value <- withVisible(function_list[[k]](value)) ## if (value[["visible"]]) ## value[["value"]] ## else invisible(value[["value"]]) ## } ## <bytecode: 0x7fd90556eb30> ## <environment: namespace:magrittr>
2.5. Generalized function composition
foo <- . %>% head() %>% tail(n = 5) # is equivalent to... foo <- function(x) { tail(head(x), n=5) }
fc
package and functionlibrary(fc) # devtools::install_github("swang87/fc") fc(tail, x = head(x), n = 5)
## function (x) ## { ## tail(x = head(x), n = 5) ## }
fc()
functionsWe can’t do the following:
iris %>% head() %>% tail(n=5)
but we can do
( head() %>% fc(tail, n=5) )(iris)
or
( head() %>% fc(tail, n=5) )(iris)
# magrittr . %>% head(n=50) %>% summary()
# fc fc(summary, object=fc(head, n = 50)(object))
## function (x) ## { ## summary(object = internal_anon_func(x)) ## } ## <environment: 0x7fd908c90618>
# Create the longitudinal data set. lupus_longitudinal = lupus_clean %>% mutate(outcome = future_map(qs, make_outcome)) %>% mutate(visits = future_map_int(outcome, nrow)) %>% filter(visits > 2) %>% mutate(last_visit = future_map_dbl(outcome, ~ .x$qsdy[nrow(.x)])) %>% mutate(sri_response = future_map_lgl(outcome, make_sri_response)) %>% mutate(bilag_ss = future_map(outcome, bilag_score_summary)) %>% select(usubjid, outcome, bilag_ss) %>% unnest() %>% select(usubjid, sledai, qsdy, pga, starts_with("bilag")) %>% mutate(sledai = as.numeric(as.character(sledai)), pga = as.numeric(as.character(pga)))
magrittr
, dplyr
, and friends work best for structured data where there is a sequence of prespecified operations.
\[ \text{standardized} \rlap{\ \ \ \not}\iff \text{tidy} \]
\[ \text{properly abstracted} \rlap{\ \ \ \not}\iff \text{tidy} \]
update_beta <- function(X, y, lambda, alpha, b, W) { WX <- W * X WX2 <- W * X^2 Xb <- X %*% b for (i in seq_along(b)) { Xb <- Xb - X[, i] * b[i] b[i] <- soft_thresh(sum(WX[,i, drop=FALSE] * (y - Xb)), lambda*alpha) b[i] <- b[i] / (sum(WX2[, i]) + lambda * (1 - alpha)) Xb <- Xb + X[, i] * b[i] } b }
Github repository https://github.com/swang87/fc