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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With