Lecture: Writing R Code (Advanced Data Analysis from an Elementary Point of View)

February 12, 2013

(This article was originally published at Three-Toed Sloth , and syndicated at StatsBlogs.)

R programs are built around functions: pieces of code that take inputs or arguments, do calculations on them, and give back outputs or return values. The most basic use of a function is to encapsulate something we've done in the terminal, so we can repeat it, or make it more flexible. To assure ourselves that the function does what we want it to do, we subject it to sanity-checks, or "write tests". To make functions more flexible, we use control structures, so that the calculation done, and not just the result, depends on the argument. R functions can call other functions; this lets us break complex problems into simpler steps, passing partial results between functions. Programs inevitably have bugs: debugging is the cycle of figuring out what the bug is, finding where it is in your code, and fixing it. Good programming habits make debugging easier, as do some tricks. Avoiding iteration. Re-writing code to avoid mistakes and confusion, to be clearer, and to be more flexible.

Reading: Notes, Appendix A

Optional reading: Slides from 36-350, introduction to statistical computing, especially through lecture 18.

Advanced Data Analysis from an Elementary Point of View

Please comment on the article here: Three-Toed Sloth