# Teach Yourself How to Create Functions in R

February 1, 2011
By

As you can tell from my previous posts, I am diving in head first into learning how to program (and simplify) my analytical life using R.  I have always learned by example and have never really prospered from the “learn from scratch” school of thought.  As I follow along with some other fellow R programmers, I find that they often use functions.  Intuitively I understand what they are and why the are awesome, yet I rarely find myself every thinking to employ them.

One reason is that I simply don’t yet fully grasp how to catch errors and debug them efficiently.  In addition, I am a very sloppy coder and don’t want to create anything “too complicated” since I am just starting out.  However, I recently was introduced to a neat little trick and I wanted to share.

Say there is a function that you often use but finding yourself wanting to make a personal change to.  Simply type the function name into the command line without any arguments!

For example:

library(gmodels)
CrossTable


For example, I currently am trying to find a quick way to automate professional- looking crosstabs that I would feel comfortable using in the appendix of a survey research project on campus.  I really like the CrossTable function inside the gmodels package, but when Sweaving the output, the size is rediculous!  Even when it print’s to the console, you can see how large it prints to other things.  Not that I am capable of figuring out how to change this quite yet, it is still good as a newbie to R to see how the function was programmed, and presumably, how to improve my R coding skills.

As an aside, I recently added my blogs post to the R-bloggers feed.  If you stumble across my posts and are too trying to learn R, you would be well off to subscribe to the RSS feed.  The link to the feed is http://www.r-bloggers.com/.

Filed under: R Tagged: R, R functions, Sweave

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...