How to pass another function as a variable to apply function in R?

Jason Mathews

So I want to apply a function over a matrix in R. The apply function takes a custom function that takes two parameters: element of a matrix and sum of the row that element belongs to. Below is the reproducible code

m = (matrix(1:10, nrow=2))
## Custom Function
IC = function(element, sum_rows){
  value = -log(element/sum_rows,2)
  return(value)
}

I am wondering if there is any way of passing rowSums as a parameter to apply function like below

apply(m,1:2,IC,sum_rows = rowSums)
Rich Scriven

When applying element-by-element (with MARGIN = 1:2), the rowSums function won't work because there are no rows. Each element is an atomic vector.

Fortunately, this operation can be fully vectorized and log2 can be used for a binary (base 2) logarithm.

-log2(m / rowSums(m))
#          [,1]     [,2]     [,3]     [,4]     [,5]
# [1,] 4.643856 3.058894 2.321928 1.836501 1.473931
# [2,] 3.906891 2.906891 2.321928 1.906891 1.584963

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass a variable from a function to another function?

From Dev

How to pass a local variable to another function?

From Dev

how to pass $(this) variable into another function javascript

From Dev

How to pass a variable from another function to the view in index function

From Dev

Pass variable to function in R

From Dev

Pass variable to function in R

From Dev

How to pass inputs from one function to another using .apply method?

From Java

R How to Pass a function as a String Inside another Function

From Dev

How to Pass Variable into Function

From Dev

Pass variable function arguments on to another function in PHP

From Dev

How to pass variable length arguments as arguments on another function in Golang?

From Dev

Ajax: How to pass variable one function to another jQuery

From Dev

How can I pass variable from one function to another in angularJS?

From Dev

How to pass variable value from one function to another

From Dev

Ajax: How to pass variable one function to another jQuery

From Dev

How can I pass variable from one function to another in angularJS?

From Dev

How to pass a function func as argument to another and then assignate this argument to a delegate variable?

From Dev

Angular 2 firebase how to pass variable to another function?

From Dev

PHP how to pass a variable-length function parameters to another variable length function?

From Dev

PHP how to pass a variable-length function parameters to another variable length function?

From Dev

How to pass the function output to another function in powershell?

From Dev

How to pass array of function pointers to another function

From Dev

how to pass function output to another function

From Dev

How to pass a function as an parameter to another function

From Dev

How to pass a function to another function in python

From Dev

How to pass function as parameter to another function?

From Dev

How to pass a generic function as parameter to another function?

From Dev

Pass variable number of arguments received to another function

From Dev

Laravel - Pass variable from a function to another

Related Related

  1. 1

    How to pass a variable from a function to another function?

  2. 2

    How to pass a local variable to another function?

  3. 3

    how to pass $(this) variable into another function javascript

  4. 4

    How to pass a variable from another function to the view in index function

  5. 5

    Pass variable to function in R

  6. 6

    Pass variable to function in R

  7. 7

    How to pass inputs from one function to another using .apply method?

  8. 8

    R How to Pass a function as a String Inside another Function

  9. 9

    How to Pass Variable into Function

  10. 10

    Pass variable function arguments on to another function in PHP

  11. 11

    How to pass variable length arguments as arguments on another function in Golang?

  12. 12

    Ajax: How to pass variable one function to another jQuery

  13. 13

    How can I pass variable from one function to another in angularJS?

  14. 14

    How to pass variable value from one function to another

  15. 15

    Ajax: How to pass variable one function to another jQuery

  16. 16

    How can I pass variable from one function to another in angularJS?

  17. 17

    How to pass a function func as argument to another and then assignate this argument to a delegate variable?

  18. 18

    Angular 2 firebase how to pass variable to another function?

  19. 19

    PHP how to pass a variable-length function parameters to another variable length function?

  20. 20

    PHP how to pass a variable-length function parameters to another variable length function?

  21. 21

    How to pass the function output to another function in powershell?

  22. 22

    How to pass array of function pointers to another function

  23. 23

    how to pass function output to another function

  24. 24

    How to pass a function as an parameter to another function

  25. 25

    How to pass a function to another function in python

  26. 26

    How to pass function as parameter to another function?

  27. 27

    How to pass a generic function as parameter to another function?

  28. 28

    Pass variable number of arguments received to another function

  29. 29

    Laravel - Pass variable from a function to another

HotTag

Archive