0

I am trying to use nested loops to iterate through a [100,12,12] array and modify each element. I am just getting an output array of all NA values. This is what my function looks like:

fcs = function(xg,wg,Gamma_d_dir,Gamma_d_dif,I_o_uc_d,I_o_uc_u,I_d_uc_d,I_d_uc_u) {
  Q = array(data=NA,dim=c(3,2,2))
  
  for (i in 1:2) {
    for (j in 1:2) {
      for (k in 1:3) {
        Q[k,j,i] = (1/pi)*Gamma_d_dir[j,i]*I_o_uc_d[k]
      }
    }
  }
  #return(Q)
  
#first collision source due to I_o_uc_u, I_d_uc_d, I_d_uc_u
  for (i in 1:2) {
    for (j in 1:2) {
      for (k in 1:3) {
        upperlimit1 = 1
        lowerlimit1 = -1
        conv11 = (upperlimit1-lowerlimit1)/2
        sum1 = 0
        for (n in 1:2) {
          upperlimit2 = 2*pi
          lowerlimit2 = 0
          conv21 = (upperlimit2-lowerlimit2)/2
          sum2 = 0
          for (m in 1:2) {
            sum2 = sum2 + wg[m]*(1/pi)*Gamma_d_dif[m,n,j,i]*(I_o_uc_u[k,m,n]+I_d_uc_d[k,m,n]+I_d_uc_u[k,m,n])
          }
          sum1 = sum1 + wg[n]*sum2*conv21
        }
        Q[k,j,i] = Q[k,j,i] + sum1*conv11
      }
    }
  }
  return(Q)
}

Within the first section of the function, if I return the first Q (the line that is commented out), I get the expected output, an array of size (100,12,12) with actual values. I have also tried returning sum1 to see if that was the problem, but sum1 gives me the expected output as well. However, when I try to return the new array Q[k,j,i] = Q[k,j,i] + sum1*conv11 I just get an array of all NAs.

I apologize that the code will not be reproducible because it draws on other functions/values. I am hoping that it is an easy fix (I'm fairly new to R). Thanks to everyone in advance, this has me completely stuck!

EDIT: Adding data for reproducing

wg = [0.047,0.107,0.160]
xg = [-0.982,-0.904,-0.769] 

Gamma_d_dir: structure(c(0.0146368656858818, 0.0146368656858818, 0.0146368656858818, 
0.0139717386533205, 0.0139717386533205, 0.0139717386533205, 0.0122837610888392, 
0.0122837610888392, 0.0122837610888392), .Dim = c(3L, 3L))

Gamma_d_dif: structure(c(0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 
0.049237490337784, 0.049237490337784, 0.049237490337784, 0.041929467456842, 
0.041929467456842, 0.041929467456842, 0.0538555492258912, 0.0538555492258912, 
0.0538555492258912, 0.049237490337784, 0.049237490337784, 0.049237490337784, 
0.041929467456842, 0.041929467456842, 0.041929467456842, 0.0538555492258912, 
0.0538555492258912, 0.0538555492258912, 0.049237490337784, 0.049237490337784, 
0.049237490337784, 0.041929467456842, 0.041929467456842, 0.041929467456842, 
0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 0.049237490337784, 
0.049237490337784, 0.049237490337784, 0.041929467456842, 0.041929467456842, 
0.041929467456842, 0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 
0.049237490337784, 0.049237490337784, 0.049237490337784, 0.041929467456842, 
0.041929467456842, 0.041929467456842, 0.0538555492258912, 0.0538555492258912, 
0.0538555492258912, 0.049237490337784, 0.049237490337784, 0.049237490337784, 
0.041929467456842, 0.041929467456842, 0.041929467456842, 0.0538555492258912, 
0.0538555492258912, 0.0538555492258912, 0.049237490337784, 0.049237490337784, 
0.049237490337784, 0.041929467456842, 0.041929467456842, 0.041929467456842, 
0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 0.049237490337784, 
0.049237490337784, 0.049237490337784, 0.041929467456842, 0.041929467456842, 
0.041929467456842, 0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 
0.049237490337784, 0.049237490337784, 0.049237490337784, 0.041929467456842, 
0.041929467456842, 0.041929467456842), .Dim = c(3L, 3L, 3L, 3L
))

I_o_uc_d: c(1.38029388938415, 1.34170990124015, 1.3042044690128, 1.26774744333388
)
             
I_o_uc_u: structure(c(0.000129755423480137, 0.000133102190791582, 0.000136535280902773, 
0.000129749291942523, 0.000133095964315359, 0.000136528958669636, 
0.000129755205207249, 0.000133101969139083, 0.000136535055841534, 
NA, NA, NA, 0.000110308754382282, 0.000110426689291562, 0.000110426689291562, 
0.000540092124278889, 0.000554022660808504, 0.000568312506128747, 
0.000540080966347844, 0.000554011478199665, 0.000568301304992292, 
0.000540092124278889, 0.000554022660808504, 0.000568312506128747, 
NA, NA, NA), .Dim = c(3L, 3L, 3L))

I_d_uc_d: structure(c(0.0922248307669918, 0.0860202770252447, 0.0802331432647984, 
0.0921979665137745, 0.0859451281500086, 0.0801163554037587, 0.0922289322269716, 
0.0860317541213116, 0.0802509856557281, 0.00757742076841967, 
0.00757741721085118, 0.00757739979310332, NA, NA, NA, 0.00186644063217427, 
0.00174087324309048, 0.00162375357472682, 0.00180599655247098, 
0.00168351440937043, 0.0015693389683829, 0.00186644063217427, 
0.00174087324309048, 0.00162375357472682, 0.00386349245322398, 
0.00388211129262134, 0.00383047289113047), .Dim = c(3L, 3L, 3L
))

I_d_uc_u: structure(c(6.03810540123238e-05, 6.19384558717535e-05, 6.35360276253899e-05, 
6.03782007311746e-05, 6.19355584113849e-05, 6.35330856050081e-05, 
6.03809524400993e-05, 6.19383527268434e-05, 6.35359228942392e-05, 
NA, NA, NA, 5.1331641312152e-05, 5.13865217474828e-05, 5.13865217474828e-05, 
0.000251329238139372, 0.000257811745429282, 0.000264461455313982, 
0.000251324045850533, 0.000257806541656752, 0.000264456242919717, 
0.000251329238139372, 0.000257811745429282, 0.000264461455313982, 
NA, NA, NA), .Dim = c(3L, 3L, 3L))

sum1 = 3.61e-05

This is what the first return of Q looks like:

structure(c(0.0064308707377685, 0.00625110565859951, 0.00607636563513547, 
0.0064308707377685, 0.00625110565859951, 0.00607636563513547, 
0.00613863973268913, 0.00596704352083312, 0.00580024401658751, 
0.00613863973268913, 0.00596704352083312, 0.00580024401658751
), .Dim = c(3L, 2L, 2L))

, , 1

            [,1]        [,2]
[1,] 0.006430871 0.006430871
[2,] 0.006251106 0.006251106
[3,] 0.006076366 0.006076366

, , 2

            [,1]        [,2]
[1,] 0.006138640 0.006138640
[2,] 0.005967044 0.005967044
[3,] 0.005800244 0.005800244
mch87
  • 31
  • 2
  • 1
    I suggest to you learn the differences between `<-` and `=` for assignments in R. https://www.roelpeters.be/the-difference-between-arrow-and-equals-assignment-in-r/ – RobertoT Nov 30 '21 at 20:14
  • R sometimes behaves a bit weird with for loops. My personal advice is to try to avoid them. There are usually other ways to implement your algorithms, however you need a deeper understand of packages and base code. – RobertoT Nov 30 '21 at 20:15
  • @RobertoT The difference between `=` and `<-` seems pretty irrelevant int this case. (Just as it is irrelevant in 99.99% of cases...). There's no confusion here between assignment and argument passing. – Gregor Thomas Nov 30 '21 at 20:16
  • Maybe you could provide an example of your Q array or just make up a new one. It will be easier! – RobertoT Nov 30 '21 at 20:16
  • Please make your question reproducible by sharing a little bit of sample data. And perhaps scaling down the problem for illustration purposes. It can be a lot easier to debug a 3x2x2 array and then generalize up to a 100x12x12 array. – Gregor Thomas Nov 30 '21 at 20:18
  • @GregorThomas Not telling this is the issue. But as far as I know it is always recommended to avoid mixing it up. – RobertoT Nov 30 '21 at 20:18
  • @RobertoT It's a style choice. We `=` users are in the minority, but I've been using `=` for about 10 years of near daily R use and it has never caused a problem. You really only run into problems with it if you do other not-recommended things like assign a value within a function call. – Gregor Thomas Nov 30 '21 at 20:20
  • @mch87 Please also share `Gamma_d_dir`, `I_o_uc_d`, `Gamma_d_dif`, and any other objects needed to reproduce the problem. (Preferably scaled down in size as well.) – Gregor Thomas Nov 30 '21 at 20:24
  • Ok @GregorThomas I didn't know that! – RobertoT Nov 30 '21 at 20:25
  • @RobertoT If you'd like to learn more, see [the FAQ on Stack Overflow](https://stackoverflow.com/q/1741820/903061). You'll note some prominent R users commenting that they prefer `=`, like Matthew Dowle, the creator of the `data.table` package. Or [John Mount's post on the topic](https://win-vector.com/2013/04/23/prefer-for-assignment-in-r/). – Gregor Thomas Nov 30 '21 at 20:29
  • Please use `dput` so we can copy-paste your data. Otherwise we have to recreate your values by hand. While you're doing that, I suggest you use `debug(fcs)` and step through your code in the debugger to work out when your NAs arrive. – dash2 Nov 30 '21 at 22:07
  • Thanks everyone for the advice! I appreciate the patience, I'm still learning best practices for posting on SO :) – mch87 Nov 30 '21 at 23:36

0 Answers0