library(tidyverse) library(edr)
c()
function.x <- c(1, 2, 3)
c()
.y <- c(x, 4, x) y
c()
.math_1 <- 5 + 5 * 2 math_2 <- (5 + 5) * 2 math_3 <- ((5 + 5) / (5 - 10)) * 2^2 c(math_1, math_2, math_3)
c()
.c( sqrt(16), log(2.5), exp(2.5), abs(-5) )
y * 2
y * y
vec_1 <- c(1, 2, 3, 4) vec_2 <- c(3, 2) vec_1 * vec_2
list( a = y[5], b = y[1:2], c = y[c(5, 1, 5)], d = y[c(1, 2, 4:5)], e = y[seq(4, 6)] )
y[y >= 3]
y >= 3
tibble()
function.example_tbl <- dplyr::tibble( col_1 = 1:10, col_2 = letters[1:10], col_3 = LETTERS[10:1] ) example_tbl
$
.example_tbl$col_1
[[
and a column name.example_tbl[["col_1"]]
[[
and a column index.example_tbl[[1]]
example_tbl[[1]] %>% {. > 8} %>% any()
summary(example_tbl)
<-
and ->
.some_letters <- letters[1:3] letters[4:6] -> more_letters c(some_letters, more_letters)
%in%
operator."d" %in% letters[4:6]
assign()
function.assign(x = "last_letters", letters[24:26]) last_letters
&
and &&
operators.a_vector <- letters[1:3] long_and <- is.numeric(a_vector) && sum(a_vector) > 1 # is.numeric(a_vector) & sum(a_vector) > 1 long_and
union()
function.letters_1 <- c("a", "b", "c", "d") letters_2 <- c("c", "d", "e", "f") union(letters_1, letters_2)
setdiff()
function.letters_1 <- c("a", "b", "c", "d") letters_2 <- c("c", "d", "e", "f") setdiff(letters_1, letters_2)
is.na()
function.mean_of_vector <- mean(c(5.3, 8.2, 3.0, NA, 3.5, 7.7)) is.na(mean_of_vector)
all.equal()
function.numbers_1 <- 6:10 - 5 numbers_2 <- 0:4 + 1 all.equal(numbers_1, numbers_2)
%%
operator.years <- c(1995, 2009, 1979) years - years %% 10
ceiling()
and floor()
functions.numeric_values <- c(5.3, 8.2, 3.0, NA, 3.5, 7.7) ceiling(numeric_values) floor(numeric_values)
rep()
function.rep(5.6, 4)
rev()
function.numeric_values
rev(numeric_values)
names()
function.named_vector <- c(first = 1.6, second = 8.9, third = NA) names(named_vector)
dim()
function.a_tibble <- dplyr::tibble(a = 1:5, b = LETTERS[4:8]) dim(a_tibble)
ifelse()
function.numbers <- c(9, 4, 3, 12) ifelse(numbers %% 2 == 0, "even", "odd")
message()
function.message("This is a message\n", "Just some helpful info")
warning()
function.warning( "This is a warning\n", "Something is not quite right", call. = FALSE )
stop()
function.stop( "The function execution has been stopped\n", "There is a serious problem", call. = FALSE )
strsplit()
function.strings <- c("part_1_of_2", "part_2_of_2") strsplit(strings, split = "_")
add_2_numbers <- function(x, y) { answer <- x + y return(answer) }
add_2_numbers(x = 3, y = 4)
add_2_numbers(x = 3)
multiply_three <- function(x, y, z = 3) { answer <- x * y * z return(answer) }
library(tidyverse) library(edr) date_str <- "2019-01-15" sales_day <- sales %>% filter(date == date_str) order_count <- unique(sales_day$order_id) %>% length() items_sold <- nrow(sales_day) total_revenue <- sales_day$price %>% sum()
sales_on_day <- function(day) { sales_day <- sales %>% filter(date == day) order_count <- unique(sales_day$order_id) %>% length() items_sold <- nrow(sales_day) total_revenue <- sales_day$price %>% sum() sales_day_tbl <- tibble( date = day, order_count = order_count, items_sold = items_sold, total_revenue = total_revenue ) return(sales_day_tbl) }
sales_on_day(day = "2019-01-15")
daily_sales <- function(sales_table) { sales_table %>% group_by(date) %>% summarize( order_count = n_distinct(order_id), items_sold = n(), total_revenue = sum(price) ) %>% arrange(desc(date)) }
daily_sales(sales_table = sales)
daily_sales <- function(sales_table, dates = NULL) { summary_tbl <- sales_table %>% group_by(date) %>% summarize( order_count = n_distinct(order_id), items_sold = n(), total_revenue = sum(price) ) %>% arrange(desc(date)) if (!is.null(dates)) { dates <- as.Date(dates) summary_tbl <- summary_tbl %>% filter(date %in% dates) } summary_tbl }
daily_sales(sales_table = sales, dates = c("2019-01-15", "2019-01-25"))
daily_sales <- function(sales_table, dates = NULL) { if (!inherits(sales_table, "data.frame")) { stop("The `sales_table` object should inherit from `data.frame`.") } summary_tbl <- sales_table %>% group_by(date) %>% summarize( order_count = n_distinct(order_id), items_sold = n(), total_revenue = sum(price) ) %>% arrange(desc(date)) if (!is.null(dates)) { stopifnot( is.character(dates) | inherits(dates, "Date"), grepl("[0-9]{4}-[0-9]{2}-[0-9]{2}", dates) ) dates <- as.Date(dates) summary_tbl <- summary_tbl %>% filter(date %in% dates) } summary_tbl }
daily_sales()
function that result in the function stopping.daily_sales(sales_table = c("2019-01-15", "2019-01-25")) daily_sales(sales_table = sales, dates = c(20190115, 20190125)) daily_sales(sales_table = sales, dates = c("209-01-15", "2019-01-25"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.