Apply function of two elements
Let say I have a vector x_n
of size n, and I want to apply a function to every element, except the first, that depends on the previous element f(x_i, x_(i-1)), how can this be done without looping?
r loops apply
add a comment |
Let say I have a vector x_n
of size n, and I want to apply a function to every element, except the first, that depends on the previous element f(x_i, x_(i-1)), how can this be done without looping?
r loops apply
3
Can we have some more info please?
– Sotos
Nov 26 '18 at 9:58
2
Note that most of the times "without looping" is just a function that hides a loop inside of it..A reproducible example would help us understand better.
– RLave
Nov 26 '18 at 10:01
iff
is vectorized, maybef(x[-1L], x[-length(x)])
?
– chinsoon12
Nov 27 '18 at 1:09
add a comment |
Let say I have a vector x_n
of size n, and I want to apply a function to every element, except the first, that depends on the previous element f(x_i, x_(i-1)), how can this be done without looping?
r loops apply
Let say I have a vector x_n
of size n, and I want to apply a function to every element, except the first, that depends on the previous element f(x_i, x_(i-1)), how can this be done without looping?
r loops apply
r loops apply
edited Nov 26 '18 at 9:58
Sotos
31.1k51741
31.1k51741
asked Nov 26 '18 at 9:53
Claudio PClaudio P
1236
1236
3
Can we have some more info please?
– Sotos
Nov 26 '18 at 9:58
2
Note that most of the times "without looping" is just a function that hides a loop inside of it..A reproducible example would help us understand better.
– RLave
Nov 26 '18 at 10:01
iff
is vectorized, maybef(x[-1L], x[-length(x)])
?
– chinsoon12
Nov 27 '18 at 1:09
add a comment |
3
Can we have some more info please?
– Sotos
Nov 26 '18 at 9:58
2
Note that most of the times "without looping" is just a function that hides a loop inside of it..A reproducible example would help us understand better.
– RLave
Nov 26 '18 at 10:01
iff
is vectorized, maybef(x[-1L], x[-length(x)])
?
– chinsoon12
Nov 27 '18 at 1:09
3
3
Can we have some more info please?
– Sotos
Nov 26 '18 at 9:58
Can we have some more info please?
– Sotos
Nov 26 '18 at 9:58
2
2
Note that most of the times "without looping" is just a function that hides a loop inside of it..A reproducible example would help us understand better.
– RLave
Nov 26 '18 at 10:01
Note that most of the times "without looping" is just a function that hides a loop inside of it..A reproducible example would help us understand better.
– RLave
Nov 26 '18 at 10:01
if
f
is vectorized, maybe f(x[-1L], x[-length(x)])
?– chinsoon12
Nov 27 '18 at 1:09
if
f
is vectorized, maybe f(x[-1L], x[-length(x)])
?– chinsoon12
Nov 27 '18 at 1:09
add a comment |
1 Answer
1
active
oldest
votes
What about this? But as noted by @RLave *apply
is just a loop in disguise:
my_fun<- function(i, x) {
if(i == 1){
return(x[i])
} else {
return(x[i] + x[i-1])
}
}
x_n<- c(10, 20, 30, 40 ,50)
sapply(1:length(x_n), my_fun, x_n)
[1] 10 30 50 70 90
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using*apply
is readability rather than speed.
– dariober
Nov 26 '18 at 11:32
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
Can you give some example?
– dariober
Nov 26 '18 at 11:41
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478520%2fapply-function-of-two-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What about this? But as noted by @RLave *apply
is just a loop in disguise:
my_fun<- function(i, x) {
if(i == 1){
return(x[i])
} else {
return(x[i] + x[i-1])
}
}
x_n<- c(10, 20, 30, 40 ,50)
sapply(1:length(x_n), my_fun, x_n)
[1] 10 30 50 70 90
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using*apply
is readability rather than speed.
– dariober
Nov 26 '18 at 11:32
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
Can you give some example?
– dariober
Nov 26 '18 at 11:41
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
add a comment |
What about this? But as noted by @RLave *apply
is just a loop in disguise:
my_fun<- function(i, x) {
if(i == 1){
return(x[i])
} else {
return(x[i] + x[i-1])
}
}
x_n<- c(10, 20, 30, 40 ,50)
sapply(1:length(x_n), my_fun, x_n)
[1] 10 30 50 70 90
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using*apply
is readability rather than speed.
– dariober
Nov 26 '18 at 11:32
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
Can you give some example?
– dariober
Nov 26 '18 at 11:41
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
add a comment |
What about this? But as noted by @RLave *apply
is just a loop in disguise:
my_fun<- function(i, x) {
if(i == 1){
return(x[i])
} else {
return(x[i] + x[i-1])
}
}
x_n<- c(10, 20, 30, 40 ,50)
sapply(1:length(x_n), my_fun, x_n)
[1] 10 30 50 70 90
What about this? But as noted by @RLave *apply
is just a loop in disguise:
my_fun<- function(i, x) {
if(i == 1){
return(x[i])
} else {
return(x[i] + x[i-1])
}
}
x_n<- c(10, 20, 30, 40 ,50)
sapply(1:length(x_n), my_fun, x_n)
[1] 10 30 50 70 90
answered Nov 26 '18 at 10:18
darioberdariober
1,1411222
1,1411222
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using*apply
is readability rather than speed.
– dariober
Nov 26 '18 at 11:32
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
Can you give some example?
– dariober
Nov 26 '18 at 11:41
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
add a comment |
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using*apply
is readability rather than speed.
– dariober
Nov 26 '18 at 11:32
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
Can you give some example?
– dariober
Nov 26 '18 at 11:41
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
So there is no performance advantage in using apply? There is not any sort of precompilation when you use it?
– Claudio P
Nov 26 '18 at 11:18
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using
*apply
is readability rather than speed.– dariober
Nov 26 '18 at 11:32
@ClaudioP No, I don't think there is much advantage. See also stackoverflow.com/questions/42393658/… Basically, the main reason for using
*apply
is readability rather than speed.– dariober
Nov 26 '18 at 11:32
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
I do get advantage in using sum or prod instead of looping though or it's false even for those?
– Claudio P
Nov 26 '18 at 11:36
Can you give some example?
– dariober
Nov 26 '18 at 11:41
Can you give some example?
– dariober
Nov 26 '18 at 11:41
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
I guess we are ot, but there is any difference between sum(x) and for( i in 1:length(x)){o=o+x[i]}?, My guess was that sum is an already compiled C function so should be faster.
– Claudio P
Nov 26 '18 at 11:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478520%2fapply-function-of-two-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Can we have some more info please?
– Sotos
Nov 26 '18 at 9:58
2
Note that most of the times "without looping" is just a function that hides a loop inside of it..A reproducible example would help us understand better.
– RLave
Nov 26 '18 at 10:01
if
f
is vectorized, maybef(x[-1L], x[-length(x)])
?– chinsoon12
Nov 27 '18 at 1:09