How to attach an R – Project Package.

If you use R any length of time, you will soon learn that many functions you want to use exist in extensions called packages.

What is a package?

The R project has a main program that includes the default function many analysts use to plot, calculate and perform common tasks. It also permits programmers to write special purpose code and distribute these as add-ons to the main code.

I discovered I needed to use “zoo”, a package that will (supposedly) make it simple to perform conditional averages. In my case, I have a of daily temperature values lasting 83 years. I’d like to calculate yearly averages; eventually, I may wish to calculate seasonal averages etc. I could program a loop, but I knew that it would be easier if someone else has already coded this, so I subscribed to the R-help mailing list and asked politely.

A nice person suggested I use “zoo” but wouldn’t tell me how to attach it. (Email lists… sigh…) However, he did make some suggestions about how I could google to find out how to attach a function. Installing turned out to be trivial; (this is likely why they guy wouldn’t answer my question.)

How I installed the R package “zoo”

In any case, here is how I attached “zoo”.
First, I read in various R documents, that I most users with internet access can download and install packages by typing ‘install(“packageName”)’, where “packageName” is the name of the package. The name of the package I want is “zoo”.

This advice was followed by a lot of very specific advice for those using Unix and no additional advice fro those using Mac’s or PC’s.

I use a mac, so, I decided I must be “most users”. I fired up “R” and type the command shown in blue below.

>install.packages(“zoo”)
— Please select a CRAN mirror for use in this session —
trying URL
‘http://cran.cnr.Berkeley.edu/bin/macosx/universal/contrib/2.4/zoo_1.3-0.tgz’
Content type ‘application/x-gzip’ length 675762 bytes
opened URL
========================

The dialog box asked me to select a CRAN mirror and window showing a list of mirrors popped open.

Install Zoo Package for R project

I’m in Illinois, so I selected “USA(IL)”.

That turned out to be a mistake. R told me that it couldn’t find “zoo” on that mirror. I fiddled around, and finally closed R, then fired it up again. I repeated the previous steps, and this time selected “USA(CA)”.

This time, R was happy and, after a pause, said this:

downloaded 659Kb
The downloaded packages are in
/tmp/RtmpocOIeY/downloaded_packages

I wasn’t quite sure I liked the idea that this is stored in a directory called “tmp”, but there were no further instructions.

How to load the R package “zoo”

Next, I read in manual discussing packages that to actually use, the functions in package, you need to load the package. To load “zoo”, I needed to type “library(zoo)”, like this:

> library(zoo)
#http://wiki.r-project.org/rwiki/doku.php?id=getting-started:installation:packages

Attaching package: ‘zoo’
The following object(s) are masked from package:base :
rapply

Typing that seems to have loaded the package; this works provided it’s already installed.

Evidently, now that I’ve installed zoo, any time I want to use zoo, I can type library(zoo), and it will be available in R. (You can also unload function when you no longer need them. This is done to conserve memory when you need to use several memory intensive packages.)

So, how do I actually use Zoo?

What can I do with “zoo” to calculate conditional averages? Beats me!

The nice guy suggested I type vignette(“zoo”), vignette(“zoo-quickref”), ?zooreg, ?aggregate.zoo for more information.

I tried vignette(“zoo”). A pdf file opened– that looks promising!

With luck, today, I’ll manage to obtain yearly average maximum temperatures for VaexJoe, Sweden.

Why not use Excel?

If I’d used Excel, I’d have annual averages by now. But my graphics wouldn’t look this nice:

Temp vs Day

(I’ll explain what that is and how I got it later.)

Also, doing other steps in any statistical analysis would begin to be slow and pesky. (Have you ever tried to select a column of 3400 cells in Excel?

In any case figuring out if the higher temperatures recently experienced in VaexJoe, Sweden are statistically significant is not time critical. (Heck, it’s not critical at all!)

So I think, in the long run, using the problem to learn R so I can apply it to other more important questions is worth the effort.