Remove isolated elements of a vectorHow to access the last value in a vector?Concatenating two...

Is there any danger of my neighbor having my wife's signature?

Coworker asking me to not bring cakes due to self control issue. What should I do?

Insecure private-key encryption

Rigorous justification for non-relativistic QM perturbation theory assumptions?

RS485 using USART or UART port on STM32

"I showed the monkey himself in the mirror". Why is this sentence grammatical?

How do you get out of your own psychology to write characters?

How long has this character been impersonating a Starfleet Officer?

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

Possible issue with my W4 and tax return

What's the reason that we have a different number of days each month?

Is the percentage symbol a constant?

Writing dialogues for characters whose first language is not English

Other than edits for international editions, did Harry Potter and the Philosopher's Stone receive errata?

Reading Mishnayos without understanding

Create linguistic diagram (in TikZ?)

How to not let the Identify spell spoil everything?

If angels and devils are the same species, why would their mortal offspring appear physically different?

Coombinatorics- The number of ways of choosing with parameters

How to align the top of the text with the top of a figure produced by tikz in minipage

Co-worker sabotaging/undoing my work (software development)

Word for something that's always reliable, but never the best?

How do I avoid the "chosen hero" feeling?

Illustrator to chemdraw



Remove isolated elements of a vector


How to access the last value in a vector?Concatenating two std::vectorsHow to find out if an item is present in a std::vector?How do I erase an element from std::vector<> by index?Test if a vector contains a given elementWhy is Java Vector (and Stack) class considered obsolete or deprecated?Counting the number of elements with the values of x in a vectorWhat is the easiest way to initialize a std::vector with hardcoded elements?Appending a vector to a vectorReshaping vector with indices in python













9















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.










share|improve this question





























    9















    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.










    share|improve this question



























      9












      9








      9








      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.










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago







      Ramiro Scorolli

















      asked 2 hours ago









      Ramiro ScorolliRamiro Scorolli

      1697




      1697
























          2 Answers
          2






          active

          oldest

          votes


















          6














          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)





          share|improve this answer

































            3














            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, abs(diff(v)) <= 4) | c(abs(diff(v)) <= 4, FALSE)]





            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              6














              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)





              share|improve this answer






























                6














                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)





                share|improve this answer




























                  6












                  6








                  6







                  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)





                  share|improve this answer















                  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)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 hours ago

























                  answered 2 hours ago









                  SotosSotos

                  30.1k51640




                  30.1k51640

























                      3














                      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, abs(diff(v)) <= 4) | c(abs(diff(v)) <= 4, FALSE)]





                      share|improve this answer




























                        3














                        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, abs(diff(v)) <= 4) | c(abs(diff(v)) <= 4, FALSE)]





                        share|improve this answer


























                          3












                          3








                          3







                          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, abs(diff(v)) <= 4) | c(abs(diff(v)) <= 4, FALSE)]





                          share|improve this answer













                          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, abs(diff(v)) <= 4) | c(abs(diff(v)) <= 4, FALSE)]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          ClemsangClemsang

                          32429




                          32429






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...

                              Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

                              VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...