Is there a way to apply a function with several arguements over an array? For instance if you had a function d<-function(a,b,c) {a+b+c} and a 4,3 array, could you apply the function over each line of the array? tapply seems to only allow one argument, and I just can't think of a way to explicitly tell R that I am passing it 3 distinct objects instead of 1 complex one. Thank you as always for your help. ~Erithid
This will do it.> a <- array(1:12, c(4, 3)) > d <- function(a,b,c) {a+b+c} > apply(a, 1, function(x) do.call("d", sapply(x, list)))[1] 15 18 21 24 -----Original Message----- From: BJ [mailto:erithid at bellsouth.net] Sent: Wednesday, May 18, 2005 9:10 AM To: r-help at stat.math.ethz.ch Subject: [R] applying a function over an array Is there a way to apply a function with several arguements over an array? For instance if you had a function d<-function(a,b,c) {a+b+c} and a 4,3 array, could you apply the function over each line of the array? tapply seems to only allow one argument, and I just can't think of a way to explicitly tell R that I am passing it 3 distinct objects instead of 1 complex one. Thank you as always for your help. ~Erithid ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On 5/18/05, BJ <erithid at bellsouth.net> wrote:> Is there a way to apply a function with several arguements over an > array? For instance if you had a function d<-function(a,b,c) {a+b+c} and > a 4,3 array, could you apply the function over each line of the array? > tapply seems to only allow one argument, and I just can't think of a way > to explicitly tell R that I am passing it 3 distinct objects instead of > 1 complex one. Thank you as always for your help. ~ErithidIf x is the 4x3 array: mapply(d, x[,1], x[,2], x[,3])
Possibly Parallel Threads
- plotting box plots on same x
- Any ideas on how to add a dotted line to a box plot to indicate a specific value?
- I never made any assumption athat anyone had any obligation to do anything
- continue for loop in case of erros
- Help using tapply with multiple variables