Some Examples of spread, gather, unite, separate.

See the R Studio Data Wrangling Cheatsheet.

See the R Studio ggplot Cheatsheet.

library(tidyverse)

The examples from the cheatsheet.

mydata <- data_frame(
  a = 1:3, 
  b = 4:6
)
mydata

Arrange the data.

arrange(mydata, desc(a)) 

Rename the data.

mydata <- rename(mydata, x = a, y = b)
mydata
arrange(mydata, desc(x)) 
mydata %>% mutate(x.prop = x/sum(x), x.cum.prop = cumsum(x)/sum(x), y.prop = y/sum(y), y.cum.prop = cumsum(y)/sum(y)) %>%
  select(x, x.prop, x.cum.prop, y, y.prop, y.cum.prop)

Example, page 27, Problem 2.2, Ott 3rd Edition

imports <- data_frame(
  Year = c(1979:1986),
  Import = c(17518,15491,19898,16663,17061,26171,23650,19650)
)
imports
imports.wide <- spread(imports, key = Year, value = Import)
imports.wide
imports.narrow <- gather(imports.wide, key = "Year.New", value = Import.New)
imports.narrow
imports.narrow <- imports.narrow %>% mutate(Year.New = as.integer(Year.New))
imports.narrow
imports %>% ggplot(aes(x=Year, y=Import)) +
  geom_col()

imports %>% ggplot(aes(x=Year, y=Import)) +
  geom_line()

Example, page 28, Problem 2.4, Ott 3rd Edition

GNP.1985 <- data_frame(
  Year = c(1985,1985,1985,1985),
  Quarter = c("I","II","III","IV"),
  GNP = c(3910,3961,4017,4067),
  DPI = c(2505,2532,2503,2533)
)
GNP.1985
GNP.1986 <- data_frame(
  Year = c(1986,1986,1986,1986),
  Quarter = c("I","II","III","IV"),
  GNP = c(4137,4203,4266,4308),
  DPI = c(2536,2555,2579,2589)
)
GNP.1986
GNP <- bind_rows(GNP.1985, GNP.1986)
GNP
GNP.wide <- GNP %>% select(Year, Quarter, GNP) %>%
  spread(key = Quarter, value = GNP ) 
GNP.wide
GNP.narrow <- GNP.wide %>%
  gather(key = Quarter, value = GNP, I,II,III, IV ) %>%
  arrange(Year)
GNP.narrow
GNP %>% ggplot(aes(x= factor(Year), y = GNP, fill = Quarter)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(x = "Year")

Example, page 30, Problem 2.14, Ott 3rd Edition

SAT <- data_frame(
  GT = c("Male, Math", "Female, Math", "Male, Verbal", "Female, Verbal"),
  "Year 1967" = c(514,467,463,486),
  "Year 1970" = c(509,465,459,461),
  "Year 1975" = c(495,449,437,431),
  "Year 1980" = c(491,443,428,420),
  "Year 1983" = c(493,445,430,420)
)
SAT
SAT.wide <- SAT %>% rename("1967" = "Year 1967", "1970" = "Year 1970", "1975" = "Year 1975", "1980" = "Year 1980", "1983" = "Year 1983")
SAT.wide
SAT.narrow <- SAT.wide %>% gather( key = Year, value = Score, "1967", "1970", "1975", "1980", "1983" )
SAT.narrow
SAT.narrow2 <- SAT.narrow %>% separate(GT, c("Gender", "Type"))
SAT.narrow2
SAT.narrow3 <- SAT.narrow2 %>% unite(GT, c("Gender", "Type"), sep="_")
SAT.narrow3
SAT.narrow %>% ggplot(aes(x=Year, y=Score, color=GT) ) +
  geom_line(aes(group = GT)) 

SAT.narrow %>% ggplot( aes( x=factor(Year), y=Score, fill=factor(GT) ) ) +
  geom_col()

SAT.narrow %>% ggplot( aes( x=factor(Year), y=Score, fill=factor(GT) ) ) +
  geom_col(position = "dodge")

LS0tCnRpdGxlOiAiVW5pdGUgRXhhbXBsZXMiCm91dHB1dDoKICBodG1sX25vdGVib29rOiBkZWZhdWx0CiAgcGRmX2RvY3VtZW50OiBkZWZhdWx0CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAotLS0KCiMgU29tZSBFeGFtcGxlcyBvZiBzcHJlYWQsIGdhdGhlciwgdW5pdGUsIHNlcGFyYXRlLgoKU2VlIHRoZSBbUiBTdHVkaW8gRGF0YSBXcmFuZ2xpbmcgQ2hlYXRzaGVldF0oaHR0cHM6Ly93d3cucnN0dWRpby5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMTUvMDIvZGF0YS13cmFuZ2xpbmctY2hlYXRzaGVldC5wZGYpLgoKU2VlIHRoZSBbUiBTdHVkaW8gZ2dwbG90IENoZWF0c2hlZXRdK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