I have a 10 x 5 data frame and a function that takes 2 inputs a and b .
a is a vector, and b is an integer.
The fun function calculates the average value of the vector a and multiplies it by b and returns the result. In the following code, I try to apply() use this function for each x column, but it does not work. Help me please!
x = data.frame(rnorm(10), rnorm(10), rnorm(10), rnorm(10), rnorm(10)) fun = function(a, b) { c = mean(a) * b return(c) } apply(x, 2, fun(x,2))
source share