Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorial for R vectorised programming [closed]

Tags:

r

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.

like image 903
Christian Avatar asked Feb 28 '23 20:02

Christian


2 Answers

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.

like image 197
Dirk Eddelbuettel Avatar answered Mar 07 '23 20:03

Dirk Eddelbuettel


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)
like image 23
Shane Avatar answered Mar 07 '23 22:03

Shane