pivot_helper | R Documentation |
A function that pretty much let's you do anything in the tidypivot space, is carefully crafted, and is adapted to make the other functions easy to use
pivot_helper(
data,
rows = NULL,
cols = NULL,
value = NULL,
wt = NULL,
fun = NULL,
prop = NULL,
within = NULL,
withinfun = NULL,
pivot = NULL,
wrap = NULL,
totals_within = NULL
)
data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
rows |
a character vector of items |
cols |
a character vector of items |
pivot |
logical: should wide table be returned - col categories as columns (TRUE), or left long and tidy (FALSE)? |
tidy_titanic %>% pivot_helper(rows = sex, cols = survived, fun = length) # pivot_count
flat_titanic %>% pivot_helper(rows = sex, value = freq, fun = mean) # pivot_calc
flat_titanic %>% pivot_helper(rows = sex, value = freq, fun = sum) # pivot_count (weighted sum)
nar <- function(x) return(NA)
flat_titanic %>% pivot_helper(rows = sex, cols = survived, fun = nar); #pivot_null
sample1 <- function(x) sample(x, 1)
flat_titanic %>% pivot_helper(rows = sex, cols = survived, fun = sample1, value = freq); #pivot_sample1
samplen <- function(x, n) paste(sample(x, 5, replace = F), collapse = ", ")
# flat_titanic %>% pivot_helper(rows = sex, cols = survived, fun = samplen, value = freq); #pivot_samplen
paste_collapse <- function(x) paste (x, collapse = ", ")
flat_titanic %>% pivot_helper(rows = sex, fun = paste_collapse, value = freq) # pivot_list
flat_titanic %>% pivot_helper(rows = sex, value = freq, prop = TRUE) # pivot_prop
flat_titanic %>% pivot_helper(rows = sex, cols = survived, value = freq, prop = TRUE)
flat_titanic %>% pivot_helper(rows = sex, cols = survived, value = freq, prop = TRUE, within = sex)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.