--- title: "Unite Examples" output: html_notebook: default pdf_document: default word_document: default --- # Some Examples of spread, gather, unite, separate. See the [R Studio Data Wrangling Cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf). See the [R Studio ggplot Cheatsheet](https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf). ```{r message=FALSE} library(tidyverse) ``` ### The examples from the cheatsheet. ```{r} mydata <- data_frame( a = 1:3, b = 4:6 ) mydata ``` Arrange the data. ```{r} arrange(mydata, desc(a)) ``` Rename the data. ```{r} mydata <- rename(mydata, x = a, y = b) mydata arrange(mydata, desc(x)) ``` ```{r} 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 ```{r} 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 ```{r} 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 ```{r} 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") ```