Example of tibble() and tribble() functions to create a data_frame.

library(tidyverse)
BP_narrow <- tibble(
  x = c("a", "b"),
  y = c(1,2),
  z = c(3.6, 8.5)
)
BP_narrow <- tribble(
  ~x, ~y,  ~z,
  "a", 2,  3.6,
  "b", 1,  8.5
)

Example of spread() and gather().

BP_narrow <- tribble(
  ~subject, ~when, ~spb,
  "BHO", "before", 160,
  "GWB", "before", 120,
  "WJC", "before", 105,
  "BHO", "after", 115,
  "GWB", "after", 135,
  "WJC", "after", 145
)
BP_wide <- BP_narrow %>% spread(key = when, value = spb)
BP_wide
BP_narrow <- BP_wide %>% gather(key = when, value = spb, after, before)
BP_narrow

Example spread()

Try the code in Section 5.2.4 on pages 101-103

library(babynames)
babynames

Example for

Try the code on pages 104-105

Example apply()

Try the code on pages 106-107

LS0tCnRpdGxlOiAiRXhhbXBsZXMiCm91dHB1dDoKICB3b3JkX2RvY3VtZW50OiBkZWZhdWx0CiAgcGRmX2RvY3VtZW50OiBkZWZhdWx0CiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAotLS0KCiMgRXhhbXBsZSBvZiAqdGliYmxlKCkqIGFuZCAqdHJpYmJsZSgpKiBmdW5jdGlvbnMgdG8gY3JlYXRlIGEgZGF0YV9mcmFtZS4KCmBgYHtyIG1lc3NhZ2U9RkFMU0V9CmxpYnJhcnkodGlkeXZlcnNlKQoKQlBfbmFycm93IDwtIHRpYmJsZSgKICB4ID0gYygiYSIsICJiIiksCiAgeSA9IGMoMSwyKSwKICB6ID0gYygzLjYsIDguNSkKKQoKQlBfbmFycm93IDwtIHRyaWJibGUoCiAgfngsIH55LCAgfnosCiAgImEiLCAyLCAgMy42LAogICJiIiwgMSwgIDguNQopCmBgYAoKCiMgRXhhbXBsZSBvZiAqc3ByZWFkKCkqIGFuZCAqZ2F0aGVyKCkqLgoKCmBgYHtyfQpCUF9uYXJyb3cgPC0gdHJpYmJsZSgKICB+c3ViamVjdCwgfndoZW4sIH5zcGIsCiAgIkJITyIsICJiZWZvcmU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