Vol. 25 No. 2 March/April 2014 19 columns Near infrared hyperspectral image analysis using R. Part 1: Introduction A.A. Gowen Aoife Gowen approached me some time ago with an idea for a new column and, not wanting to look a gift horse in the mouth, I asked her to go ahead with it. Her idea was to offer an introduction to the free, open-source software R. Although increas- ingly used by the international mathematics community, practical applications in NIR spectroscopy are rare. The first in this col- umn series is below and both Aoife and I would welcome your comments. Ed. P icture the scene: a researcher starting a project on hyperspec- tral imaging comes into my office. Student: “How can I analyse this data?” Me: “Well, there are many software pack- ages you could use: Unscrambler, Matlab, R ...”. Student: “or what?” Me: “No. R, the statistical software, not “or”. R! Haven’t you heard of it?” I think it’s fair to say that R has not enjoyed widespread use in the NIR community. This amazes me for a few reasons. First, R is a free, open-source software. Originally devel- oped back in 1993 in New Zealand by stat- isticians Ross Ijaha and Robert Gentleman (the name R coming from their own initials), it has become the most widespread sta- tistical data analysis software used world- wide. Second, as a programming language and software environment it is both flexible and straightforward to work with. Third, it is extremely useful! R thrives on a commu- nity of like-minded people who design and contribute data analysis packages for fields ranging in diversity from financial modeling to social science. I became a user during my PhD studies in 2002, using R to make predictive models for food quality. Although my research interests have diversified since then, R has remained an important tool and one I would like to share with you in this series of tutorials, geared towards those of you who perhaps want to try R but have not got round to it yet. In this issue I will go over the basics; once they are covered we will move onto hyperspectral image analysis. Downloading and installing R You are probably wondering where you can pick up your free copy of R. Just go to the cran website (cran.r-project.org), and follow the instructions. Perhaps you will want to make a cup of tea while it downloads and installs. In five minutes or less you will be ready to get started. Ready? Ok, let us start at the beginning. Once you open R, you are faced with the R graphical user interface (gui)—a screen that looks like Figure 1—and this is where all the magic happens. R is a command line programme. To speak to it, you must type commands into the command line—after the red “>” sign on the white sheet on Figure 1. In the following sections, we will generate some data and play with it—all commands will be preceded by >. Basic data analysis and plotting R comes with a few built-in packages, for doing basic things, like statistical analysis and plotting. Later we will find out how to access new packages, but for now we are going to do just a little bit of work, to see what the built-in packages offer. Let us start with a few variables, x1, x2 and x3. >x1 <- c(1:1:10) >x2 <- rnorm(10) >x3 <- rnorm(10) Note the variable assignment symbol <-” is standard for R. (The equals sign “=” works in most but not all situations!). Note also that no semicolon is required to sup- press output. Unlike in other software pack- ages, there are no pretty pictures showing us what variables exist in our workspace. So how can we monitor what variables are in the workspace? Use the ls function: >ls() This command tells us that we now have three variables, x1, x2 and x3, in our work- space. To see what data is contained in a variable you could just type its name and doi: 10.1255/nirn.1428 Figure 1. R graphical user interface.