tags:

views:

206

answers:

3

Can someone point me to a good tutorial for using vectorized programming methods in R. At the moment it feels very magical to me and I don't really understand what R's doing. Especially with regards to if statements and addressing values neighboring rows.

+3  A: 

I am not aware of a specific tutorial on vectorized programming for R.

I have a few versions of my Intro to High-Performance Computing with R tutorial here. The benefit of vectorized code is mentioned in the context of profiling, but it doesn't explain 'how to vectorize code'. I think that is hard to teach -- my best bet would be to read other people's code. Pick a few packages from CRAN and poke around.

Other than that, decent general purpose documents about R and programming in R are e.g. Pat Burns S Poetry and the more recent R Inferno.

Dirk Eddelbuettel
+2  A: 

The best way to learn this is to experiment with it since it's an interactive environment, and it's easy to create dummy data.

With regards to making comparisons in neighboring rows, the easiest thing to do is to use the - operator (which means "exclude this index") to eliminate the first and last row, as in this example:

a <- 1:10
a[5] <- 0
a[-1] > a[-length(a)] # compare each row with the preceding value

If you want to do an if statement, you have two options:

1) The if command only evaluates one value, so you need to ensure that it evaluates to TRUE/FALSE (e.g. use the all or any functions):

if(all(a[-1] > a[-length(a)])) {
  print("each row is incrementing")
} else {
  print(paste("the",which(c(FALSE, a[-1] <= a[-length(a)])),"th row isn't incrementing"))
}

2) You can do a vectorized if statement with the ifelse function. See help("ifelse") for more details. Here's an example:

ifelse(a[-1] > a[-length(a)], 1, 0)
Shane
A: 

This classic example of code optimization from R wiki can be of some help.

Paolo