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 pivot_wider() and pivot_longer().

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 %>% pivot_wider(names_from = "when", values_from = "spb")
BP_wide
BP_narrow_new <- BP_wide %>% pivot_longer(c("before", "after"), names_to = "when", values_to = "spb" )
BP_narrow_new 
LS0tCnRpdGxlOiAiRXhhbXBsZXMiCm91dHB1dDoKICB3b3JkX2RvY3VtZW50OiBkZWZhdWx0CiAgcGRmX2RvY3VtZW50OiBkZWZhdWx0CiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdAotLS0KCkluIHRoaXMgZXhhbXBsZSB3ZSB3aWxsIGxvb2sgYXQgYSBkYXRhc2V0IHRoYXQgbWlnaHQgYmUgdXNlZCB3aXRoIHRoZSBwYWlyIHQtdGVzdC4KCiMgRXhhbXBsZSBvZiAqdGliYmxlKCkqIGFuZCAqdHJpYmJsZSgpKiBmdW5jdGlvbnMgdG8gY3JlYXRlIGEgZGF0YV9mcmFtZS4KCmBgYHtyIG1lc3NhZ2U9RkFMU0V9CmxpYnJhcnkodGlkeXZlcnNlKQoKQlBfbmFycm93IDwtIHRpYmJsZSgKICB4ID0gYygiYSIsICJiIiksCiAgeSA9IGMoMSwyKSwKICB6ID0gYygzLjYsIDguNSkKKQoKQlBfbmFycm93IDwtIHRyaWJibGUoCiAgfngsIH55LCAgfnosCiAgImEiLCAyLCAgMy42LAogICJiIiwgMSwgIDguNQopCmBgYAoKCiMgRXhhbXBsZSBvZiAqcGl2b3Rfd2lkZXIoKSogYW5kICp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