In this example we will look at a dataset that might be used with the pair t-test.

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
LS0tCnRpdGxlOiAiRXhhbXBsZXMiCm91dHB1dDoKICB3b3JkX2RvY3VtZW50OiBkZWZhdWx0CiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAotLS0KCkluIHRoaXMgZXhhbXBsZSB3ZSB3aWxsIGxvb2sgYXQgYSBkYXRhc2V0IHRoYXQgbWlnaHQgYmUgdXNlZCB3aXRoIHRoZSBwYWlyIHQtdGVzdC4KCiMgRXhhbXBsZSBvZiAqdGliYmxlKCkqIGFuZCAqdHJpYmJsZSgpKiBmdW5jdGlvbnMgdG8gY3JlYXRlIGEgZGF0YV9mcmFtZS4KCmBgYHtyIG1lc3NhZ2U9RkFMU0V9CmxpYnJhcnkodGlkeXZlcnNlKQoKQlBfbmFycm93IDwtIHRpYmJsZSgKICB4ID0gYygiYSIsICJiIiksCiAgeSA9IGMoMSwyKSwKICB6ID0gYygzLjYsIDguNSkKKQoKQlBfbmFycm93IDwtIHRyaWJibGUoCiAgfngsIH55LCAgfnosCiAgImEiLCAyLCAgMy42LAogICJiIiwgMSwgIDguNQopCmBgYAoKCiMgRXhhbXBsZSBvZiAqc3ByZWFkKCkqIGFuZCAqZ2F0aGVyKCkqLgoKCmBgYHtyfQpCUF9uYXJyb3cgPC0gd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