Skip to content

Commit

Permalink
moving towards tidyverse
Browse files Browse the repository at this point in the history
See #32
  • Loading branch information
wibeasley committed Apr 2, 2017
1 parent 0a6690c commit 31bd510
Show file tree
Hide file tree
Showing 44 changed files with 457 additions and 328 deletions.
3 changes: 1 addition & 2 deletions chapter-02/chapter-02.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
rm(list=ls(all=TRUE)) #Clear the memory of variables from previous run. This is not called by knitr, because it's above the first chunk.

# ---- load-packages ------------------------------------------------------
library(knitr)
library(ggplot2)
library(wesanderson) #See https://github.com/karthik/wesanderson
library(ggplot2) #For graphing

# ---- declare-globals ------------------------------------------------------
source("./common-code/book-theme.R")
Expand Down
14 changes: 9 additions & 5 deletions chapter-02/chapter-02.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ <h2>Session Information</h2>
assertthat 0.1 2013-12-06 CRAN (R 3.3.0)
backports 1.0.5 2017-01-18 CRAN (R 3.3.1)
colorspace 1.3-2 2016-12-14 CRAN (R 3.3.1)
DBI 0.6 2017-03-09 CRAN (R 3.3.1)
devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
dichromat * 2.0-0 2013-01-24 CRAN (R 3.3.0)
digest 0.6.12 2017-01-27 CRAN (R 3.3.1)
dplyr 0.5.0 2016-06-24 CRAN (R 3.3.3)
epade * 0.3.8 2013-02-22 CRAN (R 3.3.3)
evaluate 0.10 2016-10-11 CRAN (R 3.3.1)
extrafont * 0.17 2014-12-08 CRAN (R 3.3.0)
Expand All @@ -171,26 +173,27 @@ <h2>Session Information</h2>
gridExtra * 2.2.1 2016-02-29 CRAN (R 3.3.0)
gtable 0.2.0 2016-02-26 CRAN (R 3.3.0)
highr 0.6 2016-05-09 CRAN (R 3.3.0)
hms 0.3 2016-11-22 CRAN (R 3.3.1)
htmltools 0.3.5 2016-03-21 CRAN (R 3.3.0)
htmlwidgets 0.8 2016-11-09 CRAN (R 3.3.1)
httpuv 1.3.3 2015-08-04 CRAN (R 3.3.0)
jsonlite 1.3 2017-02-28 CRAN (R 3.3.1)
knitr * 1.15.1 2016-11-22 CRAN (R 3.3.1)
labeling 0.3 2014-08-23 CRAN (R 3.3.0)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
magrittr 1.5 2014-11-22 CRAN (R 3.3.0)
MASS * 7.3-45 2016-04-21 CRAN (R 3.3.0)
magrittr * 1.5 2014-11-22 CRAN (R 3.3.0)
memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
mime 0.5 2016-07-07 CRAN (R 3.3.1)
mnormt * 1.5-5 2016-10-15 CRAN (R 3.3.1)
mnormt 1.5-5 2016-10-15 CRAN (R 3.3.1)
munsell 0.4.3 2016-02-13 CRAN (R 3.3.0)
plotrix * 3.6-4 2016-12-30 CRAN (R 3.3.3)
plyr * 1.8.4 2016-06-08 CRAN (R 3.3.0)
R6 2.2.0 2016-10-05 CRAN (R 3.3.1)
RColorBrewer * 1.1-2 2014-12-07 CRAN (R 3.3.0)
Rcpp 0.12.10 2017-03-19 CRAN (R 3.3.1)
readr 1.1.0 2017-03-22 CRAN (R 3.3.3)
reshape2 * 1.4.2 2016-10-22 CRAN (R 3.3.1)
rgl * 0.98.1 2017-03-08 CRAN (R 3.3.1)
rgl 0.98.1 2017-03-08 CRAN (R 3.3.1)
rmarkdown 1.4.0.9000 2017-04-01 Github (rstudio/rmarkdown@5f7cd3c)
rprojroot 1.2 2017-01-16 CRAN (R 3.3.1)
rsconnect 0.7 2016-12-21 CRAN (R 3.3.1)
Expand All @@ -201,12 +204,13 @@ <h2>Session Information</h2>
stringi 1.1.3 2017-03-21 CRAN (R 3.3.1)
stringr 1.2.0 2017-02-18 CRAN (R 3.3.1)
tibble 1.3.0 2017-04-01 CRAN (R 3.3.3)
tidyr 0.6.1 2017-01-10 CRAN (R 3.3.1)
wesanderson * 0.3.2 2015-01-22 CRAN (R 3.3.3)
withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
xtable 1.8-2 2016-02-05 CRAN (R 3.3.0)
yaml 2.1.14 2016-11-12 CRAN (R 3.3.1) </code></pre>
<p></details></p>
<p>Report rendered by wibeasley at 2017-04-02, 00:17 -0500 in 2 seconds.</p>
<p>Report rendered by wibeasley at 2017-04-02, 14:42 -0500 in 2 seconds.</p>
</div>
<div id="license" class="section level2">
<h2>License</h2>
Expand Down
14 changes: 9 additions & 5 deletions chapter-02/chapter-02.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ Packages -----------------------------------------------------------------------
assertthat 0.1 2013-12-06 CRAN (R 3.3.0)
backports 1.0.5 2017-01-18 CRAN (R 3.3.1)
colorspace 1.3-2 2016-12-14 CRAN (R 3.3.1)
DBI 0.6 2017-03-09 CRAN (R 3.3.1)
devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
dichromat * 2.0-0 2013-01-24 CRAN (R 3.3.0)
digest 0.6.12 2017-01-27 CRAN (R 3.3.1)
dplyr 0.5.0 2016-06-24 CRAN (R 3.3.3)
epade * 0.3.8 2013-02-22 CRAN (R 3.3.3)
evaluate 0.10 2016-10-11 CRAN (R 3.3.1)
extrafont * 0.17 2014-12-08 CRAN (R 3.3.0)
Expand All @@ -73,26 +75,27 @@ Packages -----------------------------------------------------------------------
gridExtra * 2.2.1 2016-02-29 CRAN (R 3.3.0)
gtable 0.2.0 2016-02-26 CRAN (R 3.3.0)
highr 0.6 2016-05-09 CRAN (R 3.3.0)
hms 0.3 2016-11-22 CRAN (R 3.3.1)
htmltools 0.3.5 2016-03-21 CRAN (R 3.3.0)
htmlwidgets 0.8 2016-11-09 CRAN (R 3.3.1)
httpuv 1.3.3 2015-08-04 CRAN (R 3.3.0)
jsonlite 1.3 2017-02-28 CRAN (R 3.3.1)
knitr * 1.15.1 2016-11-22 CRAN (R 3.3.1)
labeling 0.3 2014-08-23 CRAN (R 3.3.0)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
magrittr 1.5 2014-11-22 CRAN (R 3.3.0)
MASS * 7.3-45 2016-04-21 CRAN (R 3.3.0)
magrittr * 1.5 2014-11-22 CRAN (R 3.3.0)
memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
mime 0.5 2016-07-07 CRAN (R 3.3.1)
mnormt * 1.5-5 2016-10-15 CRAN (R 3.3.1)
mnormt 1.5-5 2016-10-15 CRAN (R 3.3.1)
munsell 0.4.3 2016-02-13 CRAN (R 3.3.0)
plotrix * 3.6-4 2016-12-30 CRAN (R 3.3.3)
plyr * 1.8.4 2016-06-08 CRAN (R 3.3.0)
R6 2.2.0 2016-10-05 CRAN (R 3.3.1)
RColorBrewer * 1.1-2 2014-12-07 CRAN (R 3.3.0)
Rcpp 0.12.10 2017-03-19 CRAN (R 3.3.1)
readr 1.1.0 2017-03-22 CRAN (R 3.3.3)
reshape2 * 1.4.2 2016-10-22 CRAN (R 3.3.1)
rgl * 0.98.1 2017-03-08 CRAN (R 3.3.1)
rgl 0.98.1 2017-03-08 CRAN (R 3.3.1)
rmarkdown 1.4.0.9000 2017-04-01 Github (rstudio/rmarkdown@5f7cd3c)
rprojroot 1.2 2017-01-16 CRAN (R 3.3.1)
rsconnect 0.7 2016-12-21 CRAN (R 3.3.1)
Expand All @@ -103,6 +106,7 @@ Packages -----------------------------------------------------------------------
stringi 1.1.3 2017-03-21 CRAN (R 3.3.1)
stringr 1.2.0 2017-02-18 CRAN (R 3.3.1)
tibble 1.3.0 2017-04-01 CRAN (R 3.3.3)
tidyr 0.6.1 2017-01-10 CRAN (R 3.3.1)
wesanderson * 0.3.2 2015-01-22 CRAN (R 3.3.3)
withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
xtable 1.8-2 2016-02-05 CRAN (R 3.3.0)
Expand All @@ -112,7 +116,7 @@ Packages -----------------------------------------------------------------------



Report rendered by wibeasley at 2017-04-02, 00:17 -0500 in 2 seconds.
Report rendered by wibeasley at 2017-04-02, 14:42 -0500 in 2 seconds.


## License
Expand Down
63 changes: 43 additions & 20 deletions chapter-03/chapter-03.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
rm(list=ls(all=TRUE)) #Clear the memory of variables from previous run. This is not called by knitr, because it's above the first chunk.

# ---- load-packages ------------------------------------------------------
library(knitr)
library(RColorBrewer)
library(plyr)
library(scales) #For formating values in graphs
library(ggplot2)
library(reshape2) #For convertin wide to long
library(plotrix) #For the 3D pie chart (Please notice that this package includes much more than this feature.)
library(epade) #For the 3D bar chart (Please notice that this package includes more than this feature.)

library(magrittr) #Pipes
library(ggplot2) #For graphing
requireNamespace("plyr")
requireNamespace("dplyr")
requireNamespace("scales")
requireNamespace("readr")

# ---- declare-globals ------------------------------------------------------
source("./common-code/book-theme.R")

chapterTheme <- BookTheme

# ---- load-data ------------------------------------------------------
# 'ds' stands for 'datasets'
dsPregnancy <- read.csv("./data/exercise-pregnancy.csv" , stringsAsFactors=FALSE)
dsObesity <- read.csv("./data/food-hardship-obesity.csv" , stringsAsFactors=FALSE)
dsSmoking <- read.csv("./data/smoking-tax.csv" , stringsAsFactors=FALSE)
dsPregnancy <- readr::read_csv("./data/exercise-pregnancy.csv" )
dsObesity <- readr::read_csv("./data/food-hardship-obesity.csv" )
dsSmoking <- readr::read_csv("./data/smoking-tax.csv" )

# ---- tweak-data ------------------------------------------------------
dsPregnancy$BabyWeightInKG <- dsPregnancy$BabyWeightInG / 1000

dsPregnancySummarized <- ddply(dsPregnancy, .variables="DeliveryMethod", summarize, Count=length(SubjectID))
dsPregnancySummarized$Proportion = dsPregnancySummarized$Count/sum(dsPregnancySummarized$Count)
dsPregnancySummarized$Percentage <- paste0(round(dsPregnancySummarized$Proportion*100), "%")
dsPregnancySummarized$Dummy <- c(1,1)
dsPregnancySummarized <- dsPregnancy %>%
dplyr::group_by(DeliveryMethod) %>%
dplyr::summarize(
Count = length(SubjectID)
) %>%
dplyr::ungroup() %>%
dplyr::mutate(
Proportion = Count/sum(Count),
Percentage = paste0(round(Proportion*100), "%"),
Dummy = 1L
)

dsPregnancyLong <- dsPregnancy %>%
dplyr::select(SubjectID, Group, T1Lifts, T2Lifts, T3Lifts, T4Lifts, T5Lifts) %>%
tidyr::gather(key=TimePoint, value=LiftCount, -SubjectID, -Group) %>%
dplyr::mutate(
TimePoint = as.integer(gsub(pattern="T(\\d)Lifts", "\\1", TimePoint, perl=T))
)

dsPregnancyLongSummarized <- dsPregnancyLong %>%
dplyr::group_by(TimePoint, Group) %>%
dplyr::summarize(
CountMean = mean(LiftCount, na.rm=T)
) %>%
dplyr::ungroup()

matPregnancy <- as.matrix((table(dsPregnancy$DeliveryMethod)))
# dsPregnancyMatrix <- cbind(dsPregnancyMatrix, c(1, 2), c(1,1))[, c(2,3,1)]
# matPregnancy

dsPregnancyLong <- reshape2::melt(dsPregnancy, id.vars=c("SubjectID", "Group"),
measure.vars=paste0("T", 1:5, "Lifts"),
variable.name="TimePoint", value.name="LiftCount")
# dsPregnancy$SubjectID
dsPregnancyLong$TimePoint <- as.integer(gsub(pattern="T(\\d)Lifts", "\\1", dsPregnancyLong$TimePoint, perl=T))

dsPregnancyLongSummarized <- plyr::ddply(dsPregnancyLong, .variables=c("TimePoint", "Group"), summarize, CountMean=mean(LiftCount, na.rm=T))

# ---- figure-03-01 ------------------------------------------------------
# cat("The two rotations demonstrate that the nonzero angle favors some slices more than others.")
# oldPar <- par(mfrow=c(1, 2)) #par(mfrow=c(1, 1))
Expand Down Expand Up @@ -153,12 +169,19 @@ ggplot(dsObesity, aes(x=ObesityRate)) +
labs(x="Obesity Rate (in 2011)", y="Number of States")

# ---- figure-03-12 ------------------------------------------------------
# dsPregnancyLongSummarizedFakeTable <- dsPregnancyLongSummarized %>%
# dplyr::group_by(TimePoint, Group) %>%
# dplyr::summarize(
#
# ) %>%
# dplyr::ungroup()

CreateFakeMeans <- function( d ) {
data.frame(
TimePoint = rep(d$TimePoint, times=d$CountMean),
Group = rep(d$Group, times=d$CountMean)
)}
dsPregnancyLongSummarizedFakeTable <- ddply(dsPregnancyLongSummarized, .variables=c("TimePoint", "Group"), CreateFakeMeans)
dsPregnancyLongSummarizedFakeTable <- plyr::ddply(dsPregnancyLongSummarized, .variables=c("TimePoint", "Group"), CreateFakeMeans)
oldPar <- par(mar=c(2,2,0,0))
bar.plot.ade(x="TimePoint", y="Group", data=dsPregnancyLongSummarizedFakeTable, form="c", b2=3, alpha=.5, legendon="top", ylim=c(0, 30))
par(oldPar)
Expand Down
14 changes: 9 additions & 5 deletions chapter-03/chapter-03.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,11 @@ <h2>Session Information</h2>
assertthat 0.1 2013-12-06 CRAN (R 3.3.0)
backports 1.0.5 2017-01-18 CRAN (R 3.3.1)
colorspace 1.3-2 2016-12-14 CRAN (R 3.3.1)
DBI 0.6 2017-03-09 CRAN (R 3.3.1)
devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
dichromat * 2.0-0 2013-01-24 CRAN (R 3.3.0)
digest 0.6.12 2017-01-27 CRAN (R 3.3.1)
dplyr 0.5.0 2016-06-24 CRAN (R 3.3.3)
epade * 0.3.8 2013-02-22 CRAN (R 3.3.3)
evaluate 0.10 2016-10-11 CRAN (R 3.3.1)
extrafont * 0.17 2014-12-08 CRAN (R 3.3.0)
Expand All @@ -251,26 +253,27 @@ <h2>Session Information</h2>
gridExtra * 2.2.1 2016-02-29 CRAN (R 3.3.0)
gtable 0.2.0 2016-02-26 CRAN (R 3.3.0)
highr 0.6 2016-05-09 CRAN (R 3.3.0)
hms 0.3 2016-11-22 CRAN (R 3.3.1)
htmltools 0.3.5 2016-03-21 CRAN (R 3.3.0)
htmlwidgets 0.8 2016-11-09 CRAN (R 3.3.1)
httpuv 1.3.3 2015-08-04 CRAN (R 3.3.0)
jsonlite 1.3 2017-02-28 CRAN (R 3.3.1)
knitr * 1.15.1 2016-11-22 CRAN (R 3.3.1)
labeling 0.3 2014-08-23 CRAN (R 3.3.0)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
magrittr 1.5 2014-11-22 CRAN (R 3.3.0)
MASS * 7.3-45 2016-04-21 CRAN (R 3.3.0)
magrittr * 1.5 2014-11-22 CRAN (R 3.3.0)
memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
mime 0.5 2016-07-07 CRAN (R 3.3.1)
mnormt * 1.5-5 2016-10-15 CRAN (R 3.3.1)
mnormt 1.5-5 2016-10-15 CRAN (R 3.3.1)
munsell 0.4.3 2016-02-13 CRAN (R 3.3.0)
plotrix * 3.6-4 2016-12-30 CRAN (R 3.3.3)
plyr * 1.8.4 2016-06-08 CRAN (R 3.3.0)
R6 2.2.0 2016-10-05 CRAN (R 3.3.1)
RColorBrewer * 1.1-2 2014-12-07 CRAN (R 3.3.0)
Rcpp 0.12.10 2017-03-19 CRAN (R 3.3.1)
readr 1.1.0 2017-03-22 CRAN (R 3.3.3)
reshape2 * 1.4.2 2016-10-22 CRAN (R 3.3.1)
rgl * 0.98.1 2017-03-08 CRAN (R 3.3.1)
rgl 0.98.1 2017-03-08 CRAN (R 3.3.1)
rmarkdown 1.4.0.9000 2017-04-01 Github (rstudio/rmarkdown@5f7cd3c)
rprojroot 1.2 2017-01-16 CRAN (R 3.3.1)
rsconnect 0.7 2016-12-21 CRAN (R 3.3.1)
Expand All @@ -281,12 +284,13 @@ <h2>Session Information</h2>
stringi 1.1.3 2017-03-21 CRAN (R 3.3.1)
stringr 1.2.0 2017-02-18 CRAN (R 3.3.1)
tibble 1.3.0 2017-04-01 CRAN (R 3.3.3)
tidyr 0.6.1 2017-01-10 CRAN (R 3.3.1)
wesanderson * 0.3.2 2015-01-22 CRAN (R 3.3.3)
withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
xtable 1.8-2 2016-02-05 CRAN (R 3.3.0)
yaml 2.1.14 2016-11-12 CRAN (R 3.3.1) </code></pre>
<p></details></p>
<p>Report rendered by wibeasley at 2017-04-02, 00:17 -0500 in 16 seconds.</p>
<p>Report rendered by wibeasley at 2017-04-02, 14:42 -0500 in 16 seconds.</p>
</div>
<div id="license" class="section level2">
<h2>License</h2>
Expand Down
Loading

0 comments on commit 31bd510

Please sign in to comment.