Remove isolated elements of a vector
I have a vector of integers and I want to filter it by eliminating the components that are "isolated".
What do I mean by "isolated"? those components that does not lie in an 4-neighbourhood of other component.
The components in the vector are ordered increasingly, and there are no repetitions.
For example if I have c(1,2,3,8,15,16,17)
then I need to eliminate 8
because is not in a 4-neighbourhood of other element.
I've tried applying
for (p in 1:(length(index)-2))
if((index[p+1]>3+index[p])&(index[p+2]>3+index[p+1])){index[p+1]<-0}
index<-index[index!=0]
where index
is my vector of interest, but there's some problem with the logical condition.
Could you please give me some hints?
Thanks in advance.
r if-statement vector
add a comment |
I have a vector of integers and I want to filter it by eliminating the components that are "isolated".
What do I mean by "isolated"? those components that does not lie in an 4-neighbourhood of other component.
The components in the vector are ordered increasingly, and there are no repetitions.
For example if I have c(1,2,3,8,15,16,17)
then I need to eliminate 8
because is not in a 4-neighbourhood of other element.
I've tried applying
for (p in 1:(length(index)-2))
if((index[p+1]>3+index[p])&(index[p+2]>3+index[p+1])){index[p+1]<-0}
index<-index[index!=0]
where index
is my vector of interest, but there's some problem with the logical condition.
Could you please give me some hints?
Thanks in advance.
r if-statement vector
add a comment |
I have a vector of integers and I want to filter it by eliminating the components that are "isolated".
What do I mean by "isolated"? those components that does not lie in an 4-neighbourhood of other component.
The components in the vector are ordered increasingly, and there are no repetitions.
For example if I have c(1,2,3,8,15,16,17)
then I need to eliminate 8
because is not in a 4-neighbourhood of other element.
I've tried applying
for (p in 1:(length(index)-2))
if((index[p+1]>3+index[p])&(index[p+2]>3+index[p+1])){index[p+1]<-0}
index<-index[index!=0]
where index
is my vector of interest, but there's some problem with the logical condition.
Could you please give me some hints?
Thanks in advance.
r if-statement vector
I have a vector of integers and I want to filter it by eliminating the components that are "isolated".
What do I mean by "isolated"? those components that does not lie in an 4-neighbourhood of other component.
The components in the vector are ordered increasingly, and there are no repetitions.
For example if I have c(1,2,3,8,15,16,17)
then I need to eliminate 8
because is not in a 4-neighbourhood of other element.
I've tried applying
for (p in 1:(length(index)-2))
if((index[p+1]>3+index[p])&(index[p+2]>3+index[p+1])){index[p+1]<-0}
index<-index[index!=0]
where index
is my vector of interest, but there's some problem with the logical condition.
Could you please give me some hints?
Thanks in advance.
r if-statement vector
r if-statement vector
edited Feb 25 at 8:57
RScrlli
asked Feb 25 at 8:42
RScrlliRScrlli
13511
13511
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can achieve it with a combination of outer
and colSums
, i.e.
x[colSums(abs(outer(x, x, `-`)) >= 4) == length(x)-1]
#[1] 8
To eliminate the values, we can do,
i1 <- colSums(outer(x, x, FUN = function(i, j) abs(i - j) >= 4)) == length(x) - 1
x[!i1]
#[1] 1 2 3 15 16 17
where,
x <- c(1,2,3,8,15,16,17)
add a comment |
We keep values where preceding or next difference is lower or equal to 4 :
v <- c(1,2,3,8,15,16,17)
v[c(FALSE, diff(v) <= 4) | c(diff(v) <= 4, FALSE)]
2
Neat, but since the vector is assumed to be ordered, you can even remove theabs
:)
– niko
Feb 25 at 13:23
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
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%2f54862344%2fremove-isolated-elements-of-a-vector%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
You can achieve it with a combination of outer
and colSums
, i.e.
x[colSums(abs(outer(x, x, `-`)) >= 4) == length(x)-1]
#[1] 8
To eliminate the values, we can do,
i1 <- colSums(outer(x, x, FUN = function(i, j) abs(i - j) >= 4)) == length(x) - 1
x[!i1]
#[1] 1 2 3 15 16 17
where,
x <- c(1,2,3,8,15,16,17)
add a comment |
You can achieve it with a combination of outer
and colSums
, i.e.
x[colSums(abs(outer(x, x, `-`)) >= 4) == length(x)-1]
#[1] 8
To eliminate the values, we can do,
i1 <- colSums(outer(x, x, FUN = function(i, j) abs(i - j) >= 4)) == length(x) - 1
x[!i1]
#[1] 1 2 3 15 16 17
where,
x <- c(1,2,3,8,15,16,17)
add a comment |
You can achieve it with a combination of outer
and colSums
, i.e.
x[colSums(abs(outer(x, x, `-`)) >= 4) == length(x)-1]
#[1] 8
To eliminate the values, we can do,
i1 <- colSums(outer(x, x, FUN = function(i, j) abs(i - j) >= 4)) == length(x) - 1
x[!i1]
#[1] 1 2 3 15 16 17
where,
x <- c(1,2,3,8,15,16,17)
You can achieve it with a combination of outer
and colSums
, i.e.
x[colSums(abs(outer(x, x, `-`)) >= 4) == length(x)-1]
#[1] 8
To eliminate the values, we can do,
i1 <- colSums(outer(x, x, FUN = function(i, j) abs(i - j) >= 4)) == length(x) - 1
x[!i1]
#[1] 1 2 3 15 16 17
where,
x <- c(1,2,3,8,15,16,17)
edited Feb 25 at 8:58
answered Feb 25 at 8:52
SotosSotos
31.1k51741
31.1k51741
add a comment |
add a comment |
We keep values where preceding or next difference is lower or equal to 4 :
v <- c(1,2,3,8,15,16,17)
v[c(FALSE, diff(v) <= 4) | c(diff(v) <= 4, FALSE)]
2
Neat, but since the vector is assumed to be ordered, you can even remove theabs
:)
– niko
Feb 25 at 13:23
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
add a comment |
We keep values where preceding or next difference is lower or equal to 4 :
v <- c(1,2,3,8,15,16,17)
v[c(FALSE, diff(v) <= 4) | c(diff(v) <= 4, FALSE)]
2
Neat, but since the vector is assumed to be ordered, you can even remove theabs
:)
– niko
Feb 25 at 13:23
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
add a comment |
We keep values where preceding or next difference is lower or equal to 4 :
v <- c(1,2,3,8,15,16,17)
v[c(FALSE, diff(v) <= 4) | c(diff(v) <= 4, FALSE)]
We keep values where preceding or next difference is lower or equal to 4 :
v <- c(1,2,3,8,15,16,17)
v[c(FALSE, diff(v) <= 4) | c(diff(v) <= 4, FALSE)]
edited Feb 25 at 14:04
answered Feb 25 at 9:01
ClemsangClemsang
802414
802414
2
Neat, but since the vector is assumed to be ordered, you can even remove theabs
:)
– niko
Feb 25 at 13:23
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
add a comment |
2
Neat, but since the vector is assumed to be ordered, you can even remove theabs
:)
– niko
Feb 25 at 13:23
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
2
2
Neat, but since the vector is assumed to be ordered, you can even remove the
abs
:)– niko
Feb 25 at 13:23
Neat, but since the vector is assumed to be ordered, you can even remove the
abs
:)– niko
Feb 25 at 13:23
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
@nate Done thanks !
– Clemsang
Feb 25 at 14:04
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%2f54862344%2fremove-isolated-elements-of-a-vector%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