I´m trying to replace a for
loop with a sapply
function. Inside the loop I do some optimization and therefore need the result of one optimization for the next loop.
I figured out how to use the sapply
to run the optimization but the problem is that I need to access the previous results from within the sapply
.
The following is just a random example of what I´m trying to achieve.
sapply(1:4, function(y){
r<-y
if(y!=1){z<-r[y-1]}
else{z<-9}
return(z)
})
[1,] 9 2 NA NA
What I expected to get was something like:
[1,] 9 1 2 3
What am I doing wrong? Or isn´t there any way to access previous results of iterations in sapply
?
Difference between lapply() and sapply() functions: lapply() function displays the output as a list whereas sapply() function displays the output as a vector. lapply() and sapply() functions are used to perform some operations in a list of objects.
sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set.
The real reason for this is that sapply doesn't know what your function will return without calling it. In your case the function returns a logical , but since sapply is given an empty list, the function is never called. Therefore, it has to come up with a type and it defaults to list .
The sapply () function returns the output in Vector or Matrix. The lapply () function returns the output in List. We can interchangeably use the lapply () or sapply () function to slice the data frame. Apply a function to the rows or columns or both. Apply a function to all the elements of the input object.
We can use lapply () or sapply () interchangeable to slice a data frame. We create a function, below_average (), that takes a vector of numerical values and returns a vector that only contains the values that are strictly above the average. We compare both results with the identical () function.
How to use sapply in R? In order to use the sapply function in R you will need to specify the list or vector you want to iterate over on the first argument and the function you want to apply to each element of the vector in the second. Note that you can use a function of any package or a custom function:
The apply () function applies a function to the rows or columns or both in the data frame. The lapply () function applies a function to all the elements of the input object. The sapply () function applies a function to all the elements of an input object.
Here is an example perhaps closer to the OP's use case:
f = function(x) x^2
g = function(x) abs(x)+rnorm(1)
yvec = 1:4
Here's the Reduce
approach mentioned by @Andrie:
set.seed(1)
Reduce(function(z,y) if (is.na(z)) f(y) else g(z), yvec,init=NA_real_,accumulate=TRUE)[-1]
# [1] 1.0000000 0.3735462 0.5571895 -0.2784391
And here's a common-sense loop that everyone would use (mentioned by @digEmAll):
set.seed(1)
res <- rep(NA_real_,length(yvec))
for (i in seq_along(yvec)) res[i] = if (i==1) f(yvec[i]) else g(res[i-1])
res
# [1] 1.0000000 0.3735462 0.5571895 -0.2784391
The results are the same, so, Reduce
just hides the loop, as asserted by @Roland.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With