R - where can vectorize happen? - r

So clearly one way to vectorize a function is WITHIN the function - either explicitly iterate over inputs or utilize other functions that have been vectorized. Is there a way to mark or tag a function as being/treated as vectorized so that the iteration is managed by the R platform? The analogy would be attributes in c# or annotations in Java. I tell R that this function should be treated as vectorized and it feeds that input one at a time into the function, constructing the vector output? Or am I just thinking about this whole thing incorrectly?

You can use the Vectorize function (http://stat.ethz.ch/R-manual/R-patched/library/base/html/mapply.html), to make the function take vectors.
But here it just uses the mapply function to do the vectorization. As Gavin said, you are just hiding the loop.

Related

I've vectorized a custom function, why is outer giving me an error?

Take
cubeAndAdd<-function(x,y){x^3+y^3}
outer(-1:1,-1:1,function(x,y) Vectorize(cubeAndAdd(x,y)))
Upon running this, you will get the warning message:
Warning message:
In formals(fun) : argument is not a function
Why is this? After all, if I truly wasn't using a function, then this code wouldn't run at all.
The problem comes from what you're 'feeding' to Vectorize.
Vectorize wants a function as its argument. cubeAndAdd is a function, but cubeAndAdd(x,y) is a function call.
To make your outer loop syntactically correct, you should use Vectorize to create the vectorized function, and then call that new function:
outer(-1:1,-1:1,function(x,y) Vectorize(cubeAndAdd)(x,y))
Here, Vectorize(cubeAndAdd) is the function, and you're calling it using (x,y) as arguments: so Vectorize(cubeAndAdd)(x,y)
(Although the suggestion to just remove the entire anonymous function(x,y) from the outer loop works here (and makes the one-liner shorter), it's often a good idea to explicitly 'feed' the arguments to the function, as you are doing, since this allows one to use functions that expect additional arguments).

Perform operation on subset of vector in rust?

I'm very new to Rust, and I come from C++ land. I'm trying to use the experimental Vec::partition_at_index function. I'm trying to call this function on a certain range of indices of my vector, but have it still modify the original vector (I'm implementing a version of quicksort). Is there a way this can be done?
I also noticed Iterator::partition_in_place. Is this more what I should going for? Can the iterator version be used to operate on a subset of values?
If there are C++ folks hanging out here, I'm looking for the behavior of std::partition, which can operate on an iterator range.

Can we use apply function along with some user defined function or if/while loops in R to conditionally work it on selective rows?

I know that while and if functions in R are not vectorised. while and if functions help us selectively work on some rows based on some condition. I also know that the apply function in R is used to apply over the columns and hence it operates on all rows of columns that we wish to put apply on. Can I use apply() along with user defined functions and/or with while/if loop to conditionally use it over some rows rather than all rows as apply function usually does.
Note :- This core issue here is to bypass the drawback on non-vectorization of while/if loops in R.
You can supply user defined functions to apply using an argument FUN = function(x) user_defined_function(x) {}. And apply is "vectorized" in sense that as argument it accept vectors, not scalars (but its implementation is heavily using for and if loops, type apply without arguments in your console). So for and apply are of the same perfomance.
However you can break the execution of user defined function throwing exception with stop and wrapping in tryCatch it is a non-recommended technique (it influences environements, call stacks, scopes etc., make debugging difficult and lead to errors which are difficult to identify).
Better to use for and if and very often it is the most easiest and effective way (to write a recursive function, taking in consideration that (tail) recursion is not really optimized for R, or fully refactor your algorithm quite difficult and time consuming).

Use the multiple variables in function in r

I have this function
ANN<-function (x,y){
DV<-rep(c(0:1),5)
X1<-c(1:10)
X2<-c(2:11)
ANN<-neuralnet(x~y,hidden=10,algorithm='rprop+')
return(ANN)
}
I need the function run like
formula=X1+X2
ANN(DV,formula)
and get result of the function. So the problem is to say the function USE the object which was created during the run of function. I need to run trough lapply more combinations of x,y, so I need it this way. Any advices how to achieve it? Thanks
I've edited my answer, this still works for me. Does it work for you? Can you be specific about what sort of errors you are getting?
New response:
ANN<-function (y){
X1<-c(1:10)
DV<-rep(c(0:1),5)
X2<-c(2:11)
dat <- data.frame(X1,X2)
ANN<-neuralnet(DV ~y,hidden=10,algorithm='rprop+',data=dat)
return(ANN)
}
formula<-X1+X2
ANN(formula)
If you want so specify the two parts of the formula separately, you should still pass them as formulas.
library(neuralnet)
ANN<-function (x,y){
DV<-rep(c(0:1),5)
X1<-c(1:10)
X2<-c(2:11)
formula<-update(x,y)
ANN<-neuralnet(formula,data=data.frame(DV,X1,X2),
hidden=10,algorithm='rprop+')
return(ANN)
}
ANN(DV~., ~X1+X2)
And assuming you're using neuralnet() from the neuralnet library, it seems the data= is required so you'll need to pass in a data.frame with those columns.
Formulas as special because they are not evaluated unless explicitly requested to do so. This is different than just using a symbol, where as soon as you use it is evaluated to something in the proper frame. This means there's a big difference between DV (a "name") and DV~. (a formula). The latter is safer for passing around to functions and evaluating in a different context. Things get much trickier with symbols/names.

How to wrap an R function that accepts a scalar so it will accept a vector?

I have a function in R that accepts a scalar value quite nicely, but it doesn't accept a vector.
Is there any way to place a wrapper over it, so this wrapper function can accept a vector?
The function itself has 5 parameters.
What I've tried
I've tried every combination of sapply, lapply and mapply I can think of, and R keeps on giving errors that are somewhat obscure, to say the least.
Got it.
The original function call is:
result<-MyFunc(P=34,S=100,X=100,T=1)
Method 1
To make this accept a vector input, simply add mapply in front of the function call and convert the first opening bracket ( into a comma ,:
result<-mapply(MyFunc,P=34,S=100,X=100,T=1)
Method 2
Thanks to #Roland, type ??Vectorize to find a function that can wrap a function to make it accept vectors.
MyFuncOnVector <- Vectorize(MyFunc)
result <- MyFuncOnVector(P=34,S=100,X=100,T=1)
Behind the scenes, Vectorize is calling mapply.

Resources