Purely programming point of view, I consider for-loops would be better to be avoided in R as

  • the script can be more readable
  • it is easier to handle errors

Some articles on the web indicate that looping functions (or apply family of functions) don’t guarantee faster execution and sometimes even slower. Although, assuming that the experiments are correct, in my opinion, code readability itself is beneficial enough to avoid for-loops. Even worse, R’s dynamic typing system coupled with poor readability can result in a frustrating consequence as the code grows.

Also one of the R’s best IDE (RStudio) doesn’t seem to provide an incremental debugging - it may be wrong as I’m new to it. Therefore it is not easy to debug pieces in a for-loop. In this regard, it would be a good idea to refactor for-loops into pieces.

If one has decided to avoid for-loops, the way how to code would need to be changed. With for-loops, the focus is ‘how to get the job done’. One the other hand, if it is replaced with looping functions, the focus should be ‘what does the outcome look like’. In other words, the way of thinking should be declarative, rather than imperative.

Below shows two examples from The R Project for Statistical Computing in LinkedIn. Instead of using for-loop, apply family of functions or plyr package are used for recursive computation.

The following packages are used.


How can I set up for function for the following codes?

In this post, the goal is to create a function that creates a simulated vector with the following code.

 5for(i in 1:m){
 6  x = rnorm(n)
 7  s=sum((x-mean(x))^2)
 8  v=sum(x[1:n-1]^2)
 9  sigma2[i]=s/v
10  mu[i]=mean(x)+x[n]*sqrt(sigma2[i]/n) 

The above for-loop can be replaced easily with apply and mapply as following.

 1sim <- function(n, m) {
 2  # create internal functions
 3  sigmaSquared <- function(x) {
 4    sum((x-mean(x))^2) / sum(x[1:length(x)-1]^2)
 5  }
 6  mu <- function(x) {
 7    mean(x) + x[length(x)] + sqrt(sigmaSquared(x) / length(x))
 8  }
10  # create random numbers
11  set.seed(123457)
12  mat <- mapply(rnorm, rep(n, m))
14  apply(mat, 2, mu)

The function named sim is evaluated below.

1n <- 100
2m <- 1000
3simVec <- sim(n, m)
## [1]  0.90176825 -0.55948429  0.23258654  0.08713173  0.49159634  0.71904362

Automatically selecting groups of numeric values in a data frame

  • I’m trying to extract subsets of values from my dataset which are grouped by a value (which could be any number). This column is set by another piece of software and so the code needs to be flexible enough to identify groups of identical numbers without the number being specified. I.e. if value in row10 = row11 then group. For that I have used:
1for (n in 1:length(data$BPM)){
2  for(i in 1:length(data$Date)){
3    bird<-subset(data, Date == Date[i] & BPM == BPM[n], select = c(Date:Power))
4    head(bird)
5  }
  • This seems to work. I then need to identify all of the groups which have >4 rows and separate those from each other.

Here the goal is to select records that have preset Date and BPM pairs. Also it is necessary that the numbers of each group are greater than 4.

Initial selection conditions are shown below. Note that, for simplicity, the groups will be selected only if the numbers are greater than or equal to 2.

1# create initial conditions
2numRec <- 2 # assumed to be 2 for simplicity
3dateFilter <- c(as.Date("2014/12/1"), as.Date("2014/12/3"))
4numFilter <- c(1)

Then a data frame is created.

1# create a data frame
3days <- seq(as.Date("2014/12/1"), as.Date("2014/12/3"), "day") 
4randDays <- sample(days, size=10, replace=TRUE) 
5randBpm <- sample(1:3, size=10, replace=TRUE) 
6df <- data.frame(Date=randDays, BPM = randBpm) 

At first, the data frame is converted into a list by Date and BPM using ldply.

1# data frame to list by Date and BPM
2lst <- dlply(df, .(Date, BPM))

Then dimensions of each list elements are checked using sapply. Then a Boolean vector is created so that the value will be TRUE only if the row dimension is greater than 2.

1# find which list elements >= numRec
2dimDf <- sapply(lst, dim)
3isGreater <- dimDf[1,] >= numRec

Finally the list is converted into a data frame using ldply by subsetting the Boolean vector.

1# convert into data frame only if # elements >= numRec
2exDf <- ldply(lst[isGreater])
3kable(subset(exDf, subset = Date %in% dateFilter & BPM %in% numFilter, select = c(-.id)))