X-Git-Url: https://code.communitydata.science/stats_class_2020.git/blobdiff_plain/c3ab23dc32bf58365b77cc044c6f51afa22ec5de..5adecb998e3ae7930f1c09da7490bf28d2742177:/r_tutorials/w03a-R_tutorial.html diff --git a/r_tutorials/w03a-R_tutorial.html b/r_tutorials/w03a-R_tutorial.html new file mode 100644 index 0000000..1608264 --- /dev/null +++ b/r_tutorials/w03a-R_tutorial.html @@ -0,0 +1,1699 @@ + + + + +
+ + + + + + + + + + +This supplementary tutorial goes through some additional examples and concepts that came up in our class session today (September 29, 2020). Nick and Aaron have tried to elaborate some examples that illustrate our responses.
+Note that we donât provide any new examples related to the apply()
family of functions (lapply()
, sapply()
, tapply()
) as the upcoming R tutorials will develop some additional material on those.
If any of these examples are unhelpful and/or more confusing, we strongly encourage you to reach out to us for additional clarification and/or just disregard for the time being. As they say, âyour mileage may vary.â
+One topic that came up today related into the use of chunks and breaking up functions to create more readable code.
+Letâs use openintro county data for this example.
+library(openintro)
+## Loading required package: airports
+## Loading required package: cherryblossom
+## Loading required package: usdata
+data(county)
+Letâs look at three columns: pop2000
, pop2010
, and pop2017
.
For some reason, letâs imagine that a homework assignment has asked us to compute the mean population in the years 2000, 2010, and 2017, take the median of the three means, and then take the log of that median. Why not?
+Letâs start with the mean of one columnâ¦
+mean(county$pop2000)
+## [1] NA
+Whoops, okay, letâs use that na.rm
argument again.
mean(county$pop2000, na.rm=TRUE)
+## [1] 89649.99
+Ok, now we can grab just the three columns we want. Letâs select a subset using brackets and variable names.
+three_columns <- c('pop2000', 'pop2010', 'pop2017')
+three_columns
+## [1] "pop2000" "pop2010" "pop2017"
+Hereâs a very concise one-line command that solves our hypothetical homework problem:
+log(median(sapply(county[three_columns],mean, na.rm=TRUE)))
+## [1] 11.49539
+That works fine, but it can be quite confusing to read and I canât see whatâs happening under the hood. One approach to making things more readable and transparent might involve chunks breaking things down and creating a variable for each link in the chain of functions.
+First Iâll get the 3 means.
+three_means <- sapply(county[three_columns], mean, na.rm=TRUE)
+three_means
+## pop2000 pop2010 pop2017
+## 89649.99 98262.04 103763.41
+Then I get the median of those 3 means.
+the_median <- median(three_means)
+the_median
+## [1] 98262.04
+Then I take the log to get a final answer.
+the_answer <- log(the_median)
+the_answer
+## [1] 11.49539
+Itâs the same result with nice breaks and space to insert comments that explain whatâs going on.
+Another way to make complex commands more readable is to use prettier formatting. Hereâs what that might look like:
+log(
+ median(
+ sapply(
+ county[three_columns],mean, na.rm=TRUE
+ )
+ )
+ )
+## [1] 11.49539
+Again, exactly the same output, exactly the same operations. Much easier to see how the functions are layered on top of each other.
+We also talked about prettifying code using some built-in functions in RStudio within the Code
dropdown menu. Specifically, you might look at the help documentation for the Reflow comment
, Reindent lines
, and Reformat code
menu items or just try them out. We havenât developed examples yet that can actually benefit from these commands, but hereâs a chunk of code from the upcoming R tutorial that Iâve organized âconcisely.â For now, try not to worry about what the code would do, and instead focus on how itâs formatted. Itâs very short and it works perfectly, but itâs pretty hard to figure out whatâs going on:
## messy chunk
+my.mean <- function(z){z<-z[!is.na(z)];sigma<-sum(z);n<-length(z);out.value<-sigma/n;return(out.value)}
+When I highlight that block of code and click the Reformat Code
command from the Code
dropdown menu hereâs what it looks like:
## messy chunk after reformatting
+my.mean <-
+ function(z) {
+ z <-
+ z[!is.na(z)]
+ sigma <- sum(z)
+ n <- length(z)
+ out.value <- sigma / n
+ return(out.value)
+ }
+Itâs exactly the same code. The only difference is how the text is organized. The result is far more readable.
+You can also use an option in RMarkdownâs code chunks to call tidy=TRUE
inside the beginning of the chunk within the curly brackets. The following chunk of code looks terrible in the raw .rmd fileâitâs exactly the same âmessy chunkâ a few lines up. By including the tidy=TRUE
option, it looks much better when itâs knitted.
## messy chunk with `tidy=TRUE` chunk option:
+my.mean <- function(z) {
+ z <- z[!is.na(z)]
+ sigma <- sum(z)
+ n <- length(z)
+ out.value <- sigma/n
+ return(out.value)
+}
+