0

I have a long complex array, on which I would like to do

rowsum(my_array, group = factors)

Unfortunately, rowsum does not accept complex numbers while rowSums does but does not accept factors, to average over the rows.

What would you would recommend?

Edit: example

my_array <- array(c(1+1i, -1-1i, 2, 0, 1+1i, 2+2i), dim = c(3, 2))
factors <- c(1, 1, 2)

Expected response:

     [,1] [,2]
[1,]    0 1+1i
[2,]    2 2+2i

2nd edit:

The factors are applied for each column individually and here 0 == 1+1i + (-1-1i) and 1+1i == 0 + 1+1i.

leparc
  • 133
  • 5
  • I think that this should be okay for you https://stackoverflow.com/questions/32994060/r-cumulative-sum-by-condition-with-reset – Earl Mascetti May 22 '20 at 08:00
  • Please copy and paste your complex array with the expected output too. Thank you! – MacOS May 22 '20 at 08:19
  • Please add data using `dput` and show the expected output for the same. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah May 22 '20 at 08:46
  • Thank you @SlowLearning, I have looked at `dplyr` but `group_by` does not seem applicable to complex matrices. `ave()` provides interesting output but I cannot find a simple way to obtain the solution to the example I have added. Thank you again for your help. – leparc May 22 '20 at 11:23

1 Answers1

0

my_array is

    [,1] [,2]
[1,]  1+1i 0+0i
[2,] -1-1i 1+1i
[3,]  2+0i 2+2i

From the expected response and from the 2nd edit, its clear that factors is used to split the array into the the following two arrays

 $`1`
   [,1] [,2]
   [1,]  1+1i 0+0i
   [2,] -1-1i 1+1i

   $`2`
   [,1] [,2]
   [1,] 2+0i 2+2i

and then the columns are to be summed for each array. One way to do this in base R is as follows. Use by to split the array into a list of arrays grouped by factors and then sum the columns of each array separately with colSums. colSums is used since it can process complex numbers.

y <- by(my_array, factors, FUN = colSums)

which gives the somewhat wordy but clear result:

  INDICES: 1
      V1   V2 
    0+0i 1+1i
    -------------------------------------------------------------------------------------------------------------- 
    INDICES: 2
      V1   V2 
    2+0i 2+2i

where V1 and V2 are the sums of columns of the arrays for each factor. Finally we'll pull this into an array with

t(simplify2array(y))

which gives

    V1   V2
1 0+0i 1+1i
2 2+0i 2+2i

which is the expected response.

WaltS
  • 5,410
  • 2
  • 18
  • 24
  • Thank you, and not as simple I would have thought! I am also very interested in knowing how it could work by summing rows, for instance summing over columns the transposed `t(my_array)` using the same `factors`. – leparc May 22 '20 at 17:03
  • The response I have given uses `rowsum` and not `rowSums`. In the example I gave, the (non-complex) values in the cells are summed row-wise with respect to the factors per row (not summing per column). Unfortunately, `rowsum`does not work on complex matrices (and btw I would prefer to do it per column rather than by row as I currently transpose my matrix). – leparc May 22 '20 at 19:49
  • For row 3 of your array which is associated with factor 2, the row sum can be easily calculated by entering the values in the R console. So 2 + 2+2i gives 4+2i which is the correct mathematical answer and what my row sum calculation gives. – WaltS May 22 '20 at 21:40
  • Hi @WaltS, I have tried to clarify further my initial text. – leparc May 23 '20 at 08:48
  • I think that perhaps you're using the `row sums` to mean what R would refer to a `column sums`. Anyway, I've found a simpler solution in base R which gives your expected results and is consistent which your explanation in the calculation in your 2nd edit. I've also tried to explain the steps to the result. Sorry about the confusion. – WaltS May 23 '20 at 12:48