Reputation: 315
I apologize in advance if my title is not the best way to frame the topic of my question...
Currently I use a combination of sapply
and lapply
to preallocate two lists, Ag1
and Al1
, setting upper and lower bounds on their entries based on a vector of integers A
and an integer B
. Here goes:
A = c(7,5,3,4,2)
B = 4
Ag1 = sapply(lapply(B - A, function(a) a), function(b) max(1,b))
Al1 = sapply(lapply(B + A, function(a) a), function(b) min(1,b))
Can I accomplish this more efficiently without using the combination of sapply
and lapply
?
Upvotes: 1
Views: 62
Reputation: 3240
Is the problem you're actually trying to solve more complicated? Your example can be simplified by simply combining the functions.
A = c(7,5,3,4,2)
B = 4
Ag1 = sapply(B - A, function(a) max(1, a))
Al1 = sapply(B + A, function(a) min(1, a))
> Ag1
[1] 1 1 1 1 2
> Al1
[1] 1 1 1 1 1
Upvotes: 1