-1

sweep(x, MARGIN, STATS, FUN = myfun , check.margin = TRUE, …) Is it possible to use STATS in user defined function. Here mysum

a <- matrix(1:9,3)
mysum <- function (x,param){
  print(param)
  #print(x + ax)
  return (x + param)

}
ak = c(100,200,300)
a_new <- sweep(a,2,STATS = c(1,2,3),FUN = function(x,i) mysum(x,ak[i]))

I am not able to figure how to use STATS vector into my user defined function. I am passing a second param to mysum. The examples of sweep function shows simple fucntions such as "+" "-" but no clue about how to use user defined and how to access STATS in your user defined function.

I saw some example and to pass index we pass as function(x,i) but How can do same thing in STATS function of Sweep

  • Welcome to Stack Overflow. Please [format your code](https://meta.stackexchange.com/a/22189/371738) appropriately. In addition [provide example data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) in order to make your issue reproducible. – jay.sf Jul 08 '18 at 08:59
  • I have edited my code with proper example. – ankit joshi Jul 08 '18 at 16:35

1 Answers1

0

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.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • "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." This is what I was looking for. Although I figured it out beforehand. Thank you so much explaining user defined function use. This can be very useful for someone doing large data manupulation( I had 45000 columns ;-)) – ankit joshi Jul 10 '18 at 03:02
  • @ankitjoshi No worries. Glad to help and good luck with your work:-) – Maurits Evers Jul 10 '18 at 03:06