I think there is a misunderstanding as to what sweep
does; please take a look at the post How to use the sweep function for some great examples.
The bottom line is that you need both a summary statistic (of a suitable dimension, see below) and a mathematical operation according to which you "sweep" that summary statistic from your input matrix.
In your case, the summary statistic is a vector of length length(ak) = 3
. You can therefore sweep ak
from a
using the mathematical operation defined in FUN
; how we sweep depends on MARGIN
; since a
is a 3x3
matrix, we can sweep ak
from a
either column-wise or row-wise.
In case of the former
sweep(a, 2, ak, FUN = "+")
# [,1] [,2] [,3]
#[1,] 101 204 307
#[2,] 102 205 308
#[3,] 103 206 309
and in case of the latter
sweep(a, 1, ak, FUN = "+")
# [,1] [,2] [,3]
#[1,] 101 104 107
#[2,] 202 205 208
#[3,] 303 306 309
Here we are column/row-wise sweeping by adding (FUN = "+"
) ak
to a
.
Obviously you can define your own function. For example, if we want to column-wise add ak
to the squared values of a
we can use sweep
in the following way
sweep(a, 2, ak, FUN = function(x, y) x^2 + y)
# [,1] [,2] [,3]
#[1,] 101 216 349
#[2,] 104 225 364
#[3,] 109 236 381
The first function argument x
refers to the selected vector of a
(here a column vector because MARGIN = 2
); the second function argument y
refers to the vector ak
.
It is important to ensure that dimensions "match"; for example, if we do
a <- a <- matrix(1:9,3)
ak <- c(100, 200)
sweep(a, 2, ak, FUN = "+")
we get the warning
Warning message:
In sweep(a, 2, ak, FUN = "+") :
STATS does not recycle exactly across MARGIN
as we are trying to add ak
to a
column-wise, but a
has 3 columns and ak
only 2 entries.