Reputation: 35
I have a some high dimensional arrays and want to apply some functions to them. The problem is I cannot tell apply() (or similar in apply family) that the object to pass the function on is not in the first argument of the function.
Dumb example here:
data <- c(1,2,3,4,5)
myarray <- array(data,dim=c(5,5,2,2),dimnames=list(NULL,
paste("col",1:5,sep=""),
paste("matrix",1:2,sep=""),
paste("slice",1:2,sep="")))
Now, imagine a function with this structure:
function(arg1,arg2,arg3,data,arg4)
If I want to make apply pass the function to the object "myarray", I need to specify on which argument ("data") is located. I tried this but with no results:
apply(myarray,c(3,4),function,arg1,arg2,arg3,arg4)
Thanks in advance!
Upvotes: 1
Views: 42
Reputation: 339
If I have understood your right, you need to specify the non iterating arguments. For example:
func <- function(a,b,c){
return(a*b + c)
}
apply(FUN=func,a=10,b=10,someObject)
The non specified argument is the argument that is iterated over with your specified vector.
Note that if you have a 1D structure
unlist(lapply(FUN=func,a=10,b=10,someObject))
Will likely work better.
Upvotes: 3