Study Guide for Vector Programming R

Can someone point me to a good tutorial for using vectorized programming methods in R. At the moment, it seems very magical to me, and I donโ€™t quite understand what R. does. This is especially true for if statements and addressing values โ€‹โ€‹of adjacent lines.

+4
source share
2 answers

I do not know a specific vectorized programming tutorial for R.

I have several versions of my Intro to High-Performance Computing with the R tutorial here . The advantage of vectorized code is mentioned in the context of profiling, but it does not explain "how to vectorize code." I think this is hard to learn - it will be best to read other people's code. Select several packages from CRAN and orient.

In addition, acceptable general-purpose documents on R and R programming are, for example, Pat Burns S Poetry and later R Inferno .

+6
source

The best way to find out is to experiment with it, since it is an interactive environment, and it is easy to create dummy data.

As for comparisons in adjacent lines, the easiest way is to use the - operator (which means โ€œexclude this indexโ€) to exclude the first and last lines, 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 make an if , you have two options:

1) if command evaluates only one value, so you need to make sure that it evaluates to TRUE / FALSE (for example, use 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 make a vector if statement using the ifelse function. See help("ifelse") . Here is an example:

 ifelse(a[-1] > a[-length(a)], 1, 0) 
+2
source

All Articles