I am using Oreplace function in a query, but when i use the function more than 6 times have overflow size error, any solution for this ? other funtction maby. thanks.
Related
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).
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).
I am using integrate function (in R) to numerically compute integrals. I have an univariate function with one argument f(x,a) like this (just for example purpose):
test = function(x,a) 1/sqrt(2*pi)*exp(-(x-a)^2/2)
I want to define new univariate function, which is a function of a after integrate the above function:
testa = function(a) integrate(test,0,Inf,a=a)$value #this works
Now my question is that is it possible to use integrate function on function testa ? For example:
integrate(testa,0,1) # not working
I tried and it is not working (got error message evaluation of function gave a result of wrong length). I already know that one can apply multivariate integration procedure directly on test (for example use adaptIntegrate function from cubature package). But that is not my purpose!
So does anyone know how to apply successively integrate function like the example above? Or confirm that this is not permitted in R?
Thank in advance
integrate needs a vectorized function. You can use Vectorize:
integrate(Vectorize(testa),0,1)
#0.6843731905 with absolute error < 0.00000000000022
Disclaimer: I haven't checked the result for correctness.
Sorry, that I didn't wrote the sum function in latex. I tried $$ but that didn't work...
I am a beginner in R and I want to sum up:
Sum from i=0 to n by 1.054^i
I reserached the sum() function in R. However, it seems to me that is only can just add elements and not include an index or something.
So my question is: Can I solve that with a simple sum function or do I have to use a for loop for example?
I really appreciate your answer!
UPDATE
Here is a link to my sum(sorry that I cannot post it, but I need more reputation :(link )
In R most operations are vectorized, it requires changing the mindset a bit from other languages and for this question the answer is rather than looping, you simply do the entire operation on your sequence of numbers "at once":
sum(1.054^(0:n))
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.