Using R `outer` with `%in%` operator
I am trying to perform the following outer operation:
x <- c(1, 11)
choices <- list(1:10, 10:20)
outer(x, choices, FUN=`%in%`)
I expect the following matrix:
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
which would correspond to the following operations:
outer(x, choices, FUN=paste, sep=" %in% ")
[,1] [,2]
[1,] "1 %in% 1:10" "1 %in% 10:20"
[2,] "11 %in% 1:10" "11 %in% 10:20"
But for some reason I am getting:
[,1] [,2]
[1,] FALSE FALSE
[2,] FALSE FALSE
What is happening?
r
add a comment |
I am trying to perform the following outer operation:
x <- c(1, 11)
choices <- list(1:10, 10:20)
outer(x, choices, FUN=`%in%`)
I expect the following matrix:
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
which would correspond to the following operations:
outer(x, choices, FUN=paste, sep=" %in% ")
[,1] [,2]
[1,] "1 %in% 1:10" "1 %in% 10:20"
[2,] "11 %in% 1:10" "11 %in% 10:20"
But for some reason I am getting:
[,1] [,2]
[1,] FALSE FALSE
[2,] FALSE FALSE
What is happening?
r
3
outeronly works with binary functions that are vectorized in both arguments. Thetableargument is not vectorized.
– Roland
Nov 26 '18 at 12:08
1
outer(x, choices, FUN = Vectorize(`%in%`))works, but is of course only a hidden loop.
– Roland
Nov 26 '18 at 12:12
1
I actually thoughtouterwas a (double) hidden loop, like all theapplyfunctions. I understand better now:outeris basicallyexpand.grid+FUNwhich only works ifFUNis vectorized. Makes sense. Thanks!
– antoine-sac
Nov 26 '18 at 12:49
add a comment |
I am trying to perform the following outer operation:
x <- c(1, 11)
choices <- list(1:10, 10:20)
outer(x, choices, FUN=`%in%`)
I expect the following matrix:
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
which would correspond to the following operations:
outer(x, choices, FUN=paste, sep=" %in% ")
[,1] [,2]
[1,] "1 %in% 1:10" "1 %in% 10:20"
[2,] "11 %in% 1:10" "11 %in% 10:20"
But for some reason I am getting:
[,1] [,2]
[1,] FALSE FALSE
[2,] FALSE FALSE
What is happening?
r
I am trying to perform the following outer operation:
x <- c(1, 11)
choices <- list(1:10, 10:20)
outer(x, choices, FUN=`%in%`)
I expect the following matrix:
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
which would correspond to the following operations:
outer(x, choices, FUN=paste, sep=" %in% ")
[,1] [,2]
[1,] "1 %in% 1:10" "1 %in% 10:20"
[2,] "11 %in% 1:10" "11 %in% 10:20"
But for some reason I am getting:
[,1] [,2]
[1,] FALSE FALSE
[2,] FALSE FALSE
What is happening?
r
r
asked Nov 26 '18 at 12:03
antoine-sacantoine-sac
2,78321342
2,78321342
3
outeronly works with binary functions that are vectorized in both arguments. Thetableargument is not vectorized.
– Roland
Nov 26 '18 at 12:08
1
outer(x, choices, FUN = Vectorize(`%in%`))works, but is of course only a hidden loop.
– Roland
Nov 26 '18 at 12:12
1
I actually thoughtouterwas a (double) hidden loop, like all theapplyfunctions. I understand better now:outeris basicallyexpand.grid+FUNwhich only works ifFUNis vectorized. Makes sense. Thanks!
– antoine-sac
Nov 26 '18 at 12:49
add a comment |
3
outeronly works with binary functions that are vectorized in both arguments. Thetableargument is not vectorized.
– Roland
Nov 26 '18 at 12:08
1
outer(x, choices, FUN = Vectorize(`%in%`))works, but is of course only a hidden loop.
– Roland
Nov 26 '18 at 12:12
1
I actually thoughtouterwas a (double) hidden loop, like all theapplyfunctions. I understand better now:outeris basicallyexpand.grid+FUNwhich only works ifFUNis vectorized. Makes sense. Thanks!
– antoine-sac
Nov 26 '18 at 12:49
3
3
outer only works with binary functions that are vectorized in both arguments. The table argument is not vectorized.– Roland
Nov 26 '18 at 12:08
outer only works with binary functions that are vectorized in both arguments. The table argument is not vectorized.– Roland
Nov 26 '18 at 12:08
1
1
outer(x, choices, FUN = Vectorize(`%in%`)) works, but is of course only a hidden loop.– Roland
Nov 26 '18 at 12:12
outer(x, choices, FUN = Vectorize(`%in%`)) works, but is of course only a hidden loop.– Roland
Nov 26 '18 at 12:12
1
1
I actually thought
outer was a (double) hidden loop, like all the apply functions. I understand better now: outer is basically expand.grid + FUN which only works if FUN is vectorized. Makes sense. Thanks!– antoine-sac
Nov 26 '18 at 12:49
I actually thought
outer was a (double) hidden loop, like all the apply functions. I understand better now: outer is basically expand.grid + FUN which only works if FUN is vectorized. Makes sense. Thanks!– antoine-sac
Nov 26 '18 at 12:49
add a comment |
2 Answers
2
active
oldest
votes
As expressed in the comments, the table argument of match (the function called by %in%) isn't intended to be a list (if it is, it gets coerced to a character). You should use vapply:
vapply(choices,function(y) x %in% y,logical(length(x)))
# [,1] [,2]
#[1,] TRUE FALSE
#[2,] FALSE TRUE
add a comment |
Another way that is close to your train of thought, would be to use expand.grid() to create the combinations, and then Map the two columns via %in% function, i.e.
d1 <- expand.grid(x, choices)
matrix(mapply(`%in%`, d1$Var1, d1$Var2), nrow = length(x))
#or you can use Map(`%in%`, ...) in order to keep results in a list
OR
As @nicola suggests, in order to make things better,
d1 <- expand.grid(list(x), choices)
mapply(%in%, d1$Var1, d1$Var2)
both giving,
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
1
To make things a lot better, you shouldd1 <- expand.grid(list(x), choices)and then justmapply(%in%, d1$Var1, d1$Var2)leads to the correct result, with much less time and RAM usage.
– nicola
Nov 26 '18 at 13:07
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
add a comment |
Your Answer
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%2f53480712%2fusing-r-outer-with-in-operator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As expressed in the comments, the table argument of match (the function called by %in%) isn't intended to be a list (if it is, it gets coerced to a character). You should use vapply:
vapply(choices,function(y) x %in% y,logical(length(x)))
# [,1] [,2]
#[1,] TRUE FALSE
#[2,] FALSE TRUE
add a comment |
As expressed in the comments, the table argument of match (the function called by %in%) isn't intended to be a list (if it is, it gets coerced to a character). You should use vapply:
vapply(choices,function(y) x %in% y,logical(length(x)))
# [,1] [,2]
#[1,] TRUE FALSE
#[2,] FALSE TRUE
add a comment |
As expressed in the comments, the table argument of match (the function called by %in%) isn't intended to be a list (if it is, it gets coerced to a character). You should use vapply:
vapply(choices,function(y) x %in% y,logical(length(x)))
# [,1] [,2]
#[1,] TRUE FALSE
#[2,] FALSE TRUE
As expressed in the comments, the table argument of match (the function called by %in%) isn't intended to be a list (if it is, it gets coerced to a character). You should use vapply:
vapply(choices,function(y) x %in% y,logical(length(x)))
# [,1] [,2]
#[1,] TRUE FALSE
#[2,] FALSE TRUE
answered Nov 26 '18 at 12:13
nicolanicola
18.9k21938
18.9k21938
add a comment |
add a comment |
Another way that is close to your train of thought, would be to use expand.grid() to create the combinations, and then Map the two columns via %in% function, i.e.
d1 <- expand.grid(x, choices)
matrix(mapply(`%in%`, d1$Var1, d1$Var2), nrow = length(x))
#or you can use Map(`%in%`, ...) in order to keep results in a list
OR
As @nicola suggests, in order to make things better,
d1 <- expand.grid(list(x), choices)
mapply(%in%, d1$Var1, d1$Var2)
both giving,
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
1
To make things a lot better, you shouldd1 <- expand.grid(list(x), choices)and then justmapply(%in%, d1$Var1, d1$Var2)leads to the correct result, with much less time and RAM usage.
– nicola
Nov 26 '18 at 13:07
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
add a comment |
Another way that is close to your train of thought, would be to use expand.grid() to create the combinations, and then Map the two columns via %in% function, i.e.
d1 <- expand.grid(x, choices)
matrix(mapply(`%in%`, d1$Var1, d1$Var2), nrow = length(x))
#or you can use Map(`%in%`, ...) in order to keep results in a list
OR
As @nicola suggests, in order to make things better,
d1 <- expand.grid(list(x), choices)
mapply(%in%, d1$Var1, d1$Var2)
both giving,
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
1
To make things a lot better, you shouldd1 <- expand.grid(list(x), choices)and then justmapply(%in%, d1$Var1, d1$Var2)leads to the correct result, with much less time and RAM usage.
– nicola
Nov 26 '18 at 13:07
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
add a comment |
Another way that is close to your train of thought, would be to use expand.grid() to create the combinations, and then Map the two columns via %in% function, i.e.
d1 <- expand.grid(x, choices)
matrix(mapply(`%in%`, d1$Var1, d1$Var2), nrow = length(x))
#or you can use Map(`%in%`, ...) in order to keep results in a list
OR
As @nicola suggests, in order to make things better,
d1 <- expand.grid(list(x), choices)
mapply(%in%, d1$Var1, d1$Var2)
both giving,
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
Another way that is close to your train of thought, would be to use expand.grid() to create the combinations, and then Map the two columns via %in% function, i.e.
d1 <- expand.grid(x, choices)
matrix(mapply(`%in%`, d1$Var1, d1$Var2), nrow = length(x))
#or you can use Map(`%in%`, ...) in order to keep results in a list
OR
As @nicola suggests, in order to make things better,
d1 <- expand.grid(list(x), choices)
mapply(%in%, d1$Var1, d1$Var2)
both giving,
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE TRUE
edited Nov 26 '18 at 13:13
answered Nov 26 '18 at 12:43
SotosSotos
31.1k51741
31.1k51741
1
To make things a lot better, you shouldd1 <- expand.grid(list(x), choices)and then justmapply(%in%, d1$Var1, d1$Var2)leads to the correct result, with much less time and RAM usage.
– nicola
Nov 26 '18 at 13:07
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
add a comment |
1
To make things a lot better, you shouldd1 <- expand.grid(list(x), choices)and then justmapply(%in%, d1$Var1, d1$Var2)leads to the correct result, with much less time and RAM usage.
– nicola
Nov 26 '18 at 13:07
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
1
1
To make things a lot better, you should
d1 <- expand.grid(list(x), choices) and then just mapply(%in%, d1$Var1, d1$Var2) leads to the correct result, with much less time and RAM usage.– nicola
Nov 26 '18 at 13:07
To make things a lot better, you should
d1 <- expand.grid(list(x), choices) and then just mapply(%in%, d1$Var1, d1$Var2) leads to the correct result, with much less time and RAM usage.– nicola
Nov 26 '18 at 13:07
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
@nicola I agree with everything you said. Only reason I posted this is because it is closer to OP's mindset for the specific problem. But yes, very 'RAM' consuming. Thank you for the alternative! Looks much better
– Sotos
Nov 26 '18 at 13:10
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%2f53480712%2fusing-r-outer-with-in-operator%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
outeronly works with binary functions that are vectorized in both arguments. Thetableargument is not vectorized.– Roland
Nov 26 '18 at 12:08
1
outer(x, choices, FUN = Vectorize(`%in%`))works, but is of course only a hidden loop.– Roland
Nov 26 '18 at 12:12
1
I actually thought
outerwas a (double) hidden loop, like all theapplyfunctions. I understand better now:outeris basicallyexpand.grid+FUNwhich only works ifFUNis vectorized. Makes sense. Thanks!– antoine-sac
Nov 26 '18 at 12:49