library(tidyverse)
library(edr)

7.1. Vectors in R

Listing 7.1. Creating a numeric vector with the c() function.

x <- c(1, 2, 3)

Listing 7.2. Combining numeric vectors, again with c().

y <- c(x, 4, x)
y

Listing 7.3. Performing math operations and combining the results with 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)

Listing 7.4. Using math functions and combining the results with c().

c(
  sqrt(16),
  log(2.5),
  exp(2.5),
  abs(-5)
)

Listing 7.5. Multiplying a numeric vector by a single number.

y * 2

Listing 7.6. Multiplying two numeric vectors of the same length.

y * y

Listing 7.7. Multiplying two numeric vectors of different lengths.

vec_1 <- c(1, 2, 3, 4)
vec_2 <- c(3, 2)

vec_1 * vec_2

Listing 7.8. Subsetting the same vector five different ways.

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)]
)

Listing 7.9. Subsetting a vector with a conditional statement.

y[y >= 3]

Listing 7.10. The result of a conditional statement.

y >= 3

7.2. Data Frames and Tibbles

Listing 7.11. Creating a simple tibble with the tibble() function.

example_tbl <-
  dplyr::tibble(
    col_1 = 1:10,
    col_2 = letters[1:10],
    col_3 = LETTERS[10:1]
  )

example_tbl

Listing 7.12. Extracting a vector from a tibble with $.

example_tbl$col_1

Listing 7.13. Extracting a vector from a tibble with [[ and a column name.

example_tbl[["col_1"]]

Listing 7.14. Extracting a vector from a tibble with [[ and a column index.

example_tbl[[1]]

Listing 7.15. Extracting a vector and performing a check of the values.

example_tbl[[1]] %>%
  {. > 8} %>%
  any()

Listing 7.16. Getting a summary of a tibble.

summary(example_tbl)

7.3. Useful Base R Functions

Listing 7.17. Examples: <- and ->.

some_letters <- letters[1:3]
letters[4:6] -> more_letters

c(some_letters, more_letters)

Listing 7.18. Example: %in% operator.

"d" %in% letters[4:6]

Listing 7.19. Example: assign() function.

assign(x = "last_letters", letters[24:26])
last_letters

Listing 7.20. Examples: & 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

Listing 7.21. Example: union() function.

letters_1 <- c("a", "b", "c", "d")
letters_2 <- c("c", "d", "e", "f")

union(letters_1, letters_2)

Listing 7.22. Example: setdiff() function.

letters_1 <- c("a", "b", "c", "d")
letters_2 <- c("c", "d", "e", "f")

setdiff(letters_1, letters_2)

Listing 7.23. Example: is.na() function.

mean_of_vector <- mean(c(5.3, 8.2, 3.0, NA, 3.5, 7.7))

is.na(mean_of_vector)

Listing 7.24. Example: all.equal() function.

numbers_1 <- 6:10 - 5
numbers_2 <- 0:4  + 1

all.equal(numbers_1, numbers_2)

Listing 7.25. Example: %% operator.

years <- c(1995, 2009, 1979)

years - years %% 10

Listing 7.26. Examples: ceiling() and floor() functions.

numeric_values <- c(5.3, 8.2, 3.0, NA, 3.5, 7.7)

ceiling(numeric_values)
floor(numeric_values)

Listing 7.27. Example: rep() function.

rep(5.6, 4)

Listing 7.28. Example: rev() function.

numeric_values
rev(numeric_values)

Listing 7.29. Example: names() function.

named_vector <- c(first = 1.6, second = 8.9, third = NA)

names(named_vector)

Listing 7.30. Example: dim() function.

a_tibble <- dplyr::tibble(a = 1:5, b = LETTERS[4:8])

dim(a_tibble)

Listing 7.31. Example: ifelse() function.

numbers <- c(9, 4, 3, 12)

ifelse(numbers %% 2 == 0, "even", "odd")

Listing 7.32. Example: message() function.

message("This is a message\n", "Just some helpful info")

Listing 7.33. Example: warning() function.

warning(
  "This is a warning\n",
  "Something is not quite right",
  call. = FALSE
)

Listing 7.34. Example: stop() function.

stop(
  "The function execution has been stopped\n",
  "There is a serious problem",
  call. = FALSE
)

Listing 7.35. Example: strsplit() function.

strings <- c("part_1_of_2", "part_2_of_2")

strsplit(strings, split = "_")

7.4 Writing Our Own Functions

Listing 7.36. A function that adds two values.

add_2_numbers <- function(x, y) {

  answer <- x + y
  return(answer)
}

Listing 7.37. Using our new function to get the sum of two numbers.

add_2_numbers(x = 3, y = 4)

Listing 7.38. Using our new function and supplying a single value.

add_2_numbers(x = 3)

Listing 7.39. A function with a default value.

multiply_three <- function(x, y, z = 3) {

  answer <- x * y * z
  return(answer)
}

Listing 7.40. Code to get sales data on a particular day.

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()

Listing 7.41. Putting the code into a function.

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)
}

Listing 7.42. Calling the new function to see if it works as expected.

sales_on_day(day = "2019-01-15")

Listing 7.43. Improving the function to summarize all the days.

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))
}

Listing 7.44. Getting the complete sales summary.

daily_sales(sales_table = sales)

Listing 7.45. Adding an option fetch a summary for specified days.

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
}

Listing 7.46. Getting only a partial sales summary (by choice).

daily_sales(sales_table = sales, dates = c("2019-01-15", "2019-01-25"))

Listing 7.47. How to stop the function if inputs are invalid.

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
}

Listing 7.48. Several tests of the 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"))


rich-iannone/rwr documentation built on Jan. 22, 2021, 7:51 p.m.