0

I've never used the lapply and sapply functions in R before and I have heard that they are preferred to for loops..

Here's my code:

for (i in seq(length(variable_a)))
{
  for (j in seq(length(variable_b)))
  {
    variable_c[i,j] = variable_c[i,j] + variable_b[j] * variable_d[i]
    variable_e[i,j] = variable_e[i,j] + variable_c[i,j] * variable_a[i]
  }
  variable_f[1,i] = sum(variable_c[i,])
  variable_f[2,i] = sum(variable_e[i,])
}

How do I use the apply family of functions to implement these loops? I've looked at all sorts of examples but I must be missing a concept somewhere.

Update:

There are a lot of things that happens before this nested loop, as this code is part of a simulation (not of a real world problem, more for a hobby), but I'll try to give some sample data.

All of the elements in all of the variables are numbers. variable_a is not very long, containing less than 10 elements. It's numbers can be anything greater than 0.

variable_b are also numbers greater than 0, and is made by combining several smaller variables of the same type. These smaller variables are made using different parameters and many calculations, so variable_b cannot be made in one fell swoop.

variable_d are numbers between 0 and 1. It is calculated as follows:

variable_d = variable_a
sum_d = sum(variable_d)
for (i in seq(length(variable_d)))
{
  variable_d[i] = sum_d / variable_d[i]
}
sum_d = sum(variable_d)
for (i in seq(length(variable_d)))
{
  variable_d[i] = variable_d[i] / sum_d
}

In effect, the higher any given element in variable_a is, the lower the corresponding element in variable_d will be.

variable_f is what is returned to another function

variable_c and variable_e are initialized as arrays as follows

variable_c = array(0, dim = c(length(variable_a), some_other_variable_created_earlier_here)

Normally, the some_other_variable_created_earlier_here is an integer of in the millions, so I will provide a much abbreviated list here.

variable_a[1] = 37.2

variable_b = 0 0 0 0 0 0 0 0 0 0 1 0 2

variable_d = 1 (if variable_a[1] = 30 and variable_a[2] = 90, then variable_d = 0.75 0.25)

variables_c and e are arrays

I hope this helps.

Alpha Bravo
  • 170
  • 12
  • Welcome to SO. A standars syntax might be `lapply(df, functio(x) sapply(x, function )` but it depends on you data and on what type of objects you are dealing with. For next questions try to help us to provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and/or a desired output. – SabDeM Jul 27 '15 at 16:32
  • `apply` functions are preferred to `for` loops mostly because `for` loops don't manage memory very well. This is particularly true when you create new objects within your `for` loop. (at least by my understanding). In your case, it looks like your variables already exist outside of the loop, so there's little be to gained in translating this into apply functions. For reference, see the 4th circle of hell in the R Inferno. (http://www.burns-stat.com/pages/Tutor/R_inferno.pdf) – Benjamin Jul 27 '15 at 16:40
  • Okay, that's good to know. I was actually hoping that there was a computation speed up by using those functions instead of the for loop. – Alpha Bravo Jul 27 '15 at 16:44
  • Can you describe in words what your algorithm does? Or provide sample data so we can reproduce? A glance at your code lets me think you can do all of this with simple matrix operations without any loops at all. – Andrie Jul 27 '15 at 17:07
  • 1
    I agree with @Benjamin, but I'd like to add that in most cases you can gain performance by avoiding nested `for`loops. However, such a gain is typically not achieved directly by replacing the `for` loop with a member of the `*apply` family, but by simply avoiding one of the loops. The vectorized operations of `R` often allow you to treat all elements of a vector or even a matrix without addressing sequentially each element, as it is usually done, e.g., in C or Fortran. One of the advantages of the `*apply` functions is that they allow for an elegant and compact coding that is less error-prone – RHertel Jul 27 '15 at 17:32

0 Answers0