Archive for the 'R' Category

How to Scale a Vector of Numbers to One

Let’s say you have a vector of numbers: 1, 2, and 5. And let’s say you have another vector of numbers: 3, 4, 9. These vectors have different ranges, but you believe they actually should have the same range. This scenario probably sounds strange, but it can happen, for example, in cases where genetic data [...] Read more »

When Warnings Don’t Print in R

When you’re running R from the command line (using Rscript), sometimes you might get a message saying that warnings have occurred but weren’t printed to the screen. Sometimes you need to worry about the warnings, and sometimes you don’t. That’s why they’re warnings and not errors. Anyway, when you get that message, you can try [...] Read more »

For loops in R: how to do a “continue”

In R, you can use “for” loops just like in most other programming languages. Sometimes when you are looping through items and you encounter a certain condition, you want to skip over an item and move to the next item in the loop. In some languages, the keyword for doing this is “continue.” In R, [...] Read more »

Putting Labels Above Bars in Barplots in R

Let’s say you are creating a barplot in R. Sometimes you want to label the bars below the X axis. But sometimes you might also want to put a label above the bars. This is pretty easy to do but a little hidden. Below is a working example based on randomly generated data that illustrates [...] Read more »

Solving “cannot find xml2-config” on Ubuntu 9.10

I was trying to install some packages in R recently that depended on the XML package. I used the nifty install.packages() command, but I was getting an error:
“cannot find xml2-config”
This error was preventing these other packages from being installed properly. It turns out that I needed to install a library called libxml2-dev. In my case, [...] Read more »

How to Create a Simple ROC Curve

I’m using the R statistical package and the ROCR package within that. Both of these are free and very flexible. Sometimes with that flexibility comes ambiguity as to what you should do to accomplish a relatively simple task. I am doing a data-mining (machine-learning) project in which I predict a cancer patient’s prognosis. The algorithms [...] Read more »

How to Access Slots in S4 Classes in R: Area Under Curve Example

In the R statistical package, you have various ways of representing and packaging data. The most simple is in a single variable. More complex representations include vectors, lists, matrices, and data frames. An even more complex representation is S4 Classes, which are intended to simulate object-oriented programming in R.
I was using an R package that [...] Read more »

Graph of pch Plotting Symbols in R

In R, you can plot a variety of symbols when you are plotting points on a graph. The default is a hollow circle. But it is very flexible. To do this, you use the pch parameter when you create a plot. You can find information in the help files (?pch), but it only describes the [...] Read more »

How to Run an R Script from the Command Line

I’m writing this post because I just spent a couple of hours banging my head against the wall, trying to figure out how to run an R script from the command line. It was working if I simply ran it at the command line. But when I try to run the same command from Java [...] Read more »

Dynamically Invoke a Function in R

One way the R programming language has been described is that it is a functional programming language. Whether it would be called this by purists, I don’t know. But part of what this means is that all functions are treated as objects. So you can pass functions around very easily. This might sound strange, but [...] Read more »

How to List Files in a Directory Using Wildcards in R

Let’s say you have a directory with files matching a certain pattern (and others that don’t), and you want to find all the files that match that pattern in R. It’s pretty easy to do this, but it’s not super straightforward to figure it out from the help. Below is the simple answer. But you [...] Read more »

How to Do a String Join in R

Let’s say you have a list of values in a vector. And you want to be able to convert that into a delimited string. In some languages (such as Python), you can do this easily with the join method. But how would you do this in R?
This can be done with the paste function. The [...] Read more »

Modifying Text Size of Axis Labels in R

When you create a plot in R, you can easily modify the text size of the labels on the axes using the cex.lab property. This stands for “character expansion of labels.” For this value, you specify a relative size (compared to the default) that you want the text to be. The following code shows how [...] Read more »

What Is the R Statistical Package?

R is an open source statistical package that can be used to do complex analyses and produce publication quality graphics. It’s home page is at http://www.r-project.org.
This tool is very powerful but sometimes hard to figure out how to do simple things. For this reason, I will be posting some tips as I encounter such situations. Read more »

Rotate Axis Names on X-Axis in R

You use one of the par parameters to do this. For example:

x = c(3,4,2,5)
names(x) = c(“ABC”, “DEF”, “GHI”, “JKL”)
barplot(x, las=”2″)

See also: What Is the R Statistical Package Read more »