Vector analysis in R











up vote
1
down vote

favorite












As inputs your function should take a vector of 0s and 1s;
Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
To help you social planner provides some examples of what your function should return:



#Input: c(1,1,1,1,0,0,0,0)

#Output: 1 1 1 1 1 1 1 1

#Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)

#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2

#Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)

#Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2


Functions, which might be helpful:
diff()
cumsum()
which()
rle()



I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner










share|improve this question


























    up vote
    1
    down vote

    favorite












    As inputs your function should take a vector of 0s and 1s;
    Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
    Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
    To help you social planner provides some examples of what your function should return:



    #Input: c(1,1,1,1,0,0,0,0)

    #Output: 1 1 1 1 1 1 1 1

    #Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)

    #Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2

    #Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)

    #Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2


    Functions, which might be helpful:
    diff()
    cumsum()
    which()
    rle()



    I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      As inputs your function should take a vector of 0s and 1s;
      Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
      Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
      To help you social planner provides some examples of what your function should return:



      #Input: c(1,1,1,1,0,0,0,0)

      #Output: 1 1 1 1 1 1 1 1

      #Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)

      #Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2

      #Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)

      #Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2


      Functions, which might be helpful:
      diff()
      cumsum()
      which()
      rle()



      I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner










      share|improve this question













      As inputs your function should take a vector of 0s and 1s;
      Every time you see a sequence of 1s in the data you need to increase the number of children by 1;
      Be careful with the two subsequent sequences of 1s, where the difference between them is less than 5 (i.e. when there are less than 5 0s in between them, then it is the same child and not a new child);
      To help you social planner provides some examples of what your function should return:



      #Input: c(1,1,1,1,0,0,0,0)

      #Output: 1 1 1 1 1 1 1 1

      #Input: c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)

      #Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2

      #Input: c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)

      #Output: 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2


      Functions, which might be helpful:
      diff()
      cumsum()
      which()
      rle()



      I dont quite understand how to approach the question, my thoughts on this are using diff function after the cumsum as it will help me to sustain a row of 1s but in this scenario i am loosing the length of vector (it obviously becomes shorter) also #rle$lenght seems to help me to detect gaps of length 5 or more to turn 1s into 2s. Sorry for this question I am only a beginner







      r rstudio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 8:50









      Дмитрий Омегов

      61




      61
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:



          vector_analyse <- function(sample_vector){
          # ----------------------------------------------------------------------------
          # Signature: vector --> vector
          # Author: kon_u
          # Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
          # the data you need to increase the number of children by 1 (when there are less
          # 5 0s in between them, then it is the same child and not a new child)
          # ----------------------------------------------------------------------------

          # ----------------------------------------------------------------------------
          # Run Length Encoding gives a list of length and values
          # ----------------------------------------------------------------------------
          rle_object <- rle(sample_vector)
          x <- rle_object$lengths # original length
          y <- rle_object$values # original values
          z <- which(y == 1) # index of 1 in vector y
          if (length(z) == 1){
          invisible()
          } else{
          for (i in 2:length(z)){
          if (x[z[i]-1] >= 5){
          y[z[i]] = y[z[i]]
          } else {
          y[z[i]] = y[z[i]] - 1
          }
          }
          }
          y_cumsum = cumsum(y)
          rle_object$values <- y_cumsum
          new_vector = inverse.rle(rle_object)
          return(new_vector)
          }

          vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
          vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
          vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2





          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',
            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%2f53247149%2fvector-analysis-in-r%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








            up vote
            0
            down vote













            I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:



            vector_analyse <- function(sample_vector){
            # ----------------------------------------------------------------------------
            # Signature: vector --> vector
            # Author: kon_u
            # Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
            # the data you need to increase the number of children by 1 (when there are less
            # 5 0s in between them, then it is the same child and not a new child)
            # ----------------------------------------------------------------------------

            # ----------------------------------------------------------------------------
            # Run Length Encoding gives a list of length and values
            # ----------------------------------------------------------------------------
            rle_object <- rle(sample_vector)
            x <- rle_object$lengths # original length
            y <- rle_object$values # original values
            z <- which(y == 1) # index of 1 in vector y
            if (length(z) == 1){
            invisible()
            } else{
            for (i in 2:length(z)){
            if (x[z[i]-1] >= 5){
            y[z[i]] = y[z[i]]
            } else {
            y[z[i]] = y[z[i]] - 1
            }
            }
            }
            y_cumsum = cumsum(y)
            rle_object$values <- y_cumsum
            new_vector = inverse.rle(rle_object)
            return(new_vector)
            }

            vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
            vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
            vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2





            share|improve this answer

























              up vote
              0
              down vote













              I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:



              vector_analyse <- function(sample_vector){
              # ----------------------------------------------------------------------------
              # Signature: vector --> vector
              # Author: kon_u
              # Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
              # the data you need to increase the number of children by 1 (when there are less
              # 5 0s in between them, then it is the same child and not a new child)
              # ----------------------------------------------------------------------------

              # ----------------------------------------------------------------------------
              # Run Length Encoding gives a list of length and values
              # ----------------------------------------------------------------------------
              rle_object <- rle(sample_vector)
              x <- rle_object$lengths # original length
              y <- rle_object$values # original values
              z <- which(y == 1) # index of 1 in vector y
              if (length(z) == 1){
              invisible()
              } else{
              for (i in 2:length(z)){
              if (x[z[i]-1] >= 5){
              y[z[i]] = y[z[i]]
              } else {
              y[z[i]] = y[z[i]] - 1
              }
              }
              }
              y_cumsum = cumsum(y)
              rle_object$values <- y_cumsum
              new_vector = inverse.rle(rle_object)
              return(new_vector)
              }

              vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
              vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
              vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:



                vector_analyse <- function(sample_vector){
                # ----------------------------------------------------------------------------
                # Signature: vector --> vector
                # Author: kon_u
                # Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
                # the data you need to increase the number of children by 1 (when there are less
                # 5 0s in between them, then it is the same child and not a new child)
                # ----------------------------------------------------------------------------

                # ----------------------------------------------------------------------------
                # Run Length Encoding gives a list of length and values
                # ----------------------------------------------------------------------------
                rle_object <- rle(sample_vector)
                x <- rle_object$lengths # original length
                y <- rle_object$values # original values
                z <- which(y == 1) # index of 1 in vector y
                if (length(z) == 1){
                invisible()
                } else{
                for (i in 2:length(z)){
                if (x[z[i]-1] >= 5){
                y[z[i]] = y[z[i]]
                } else {
                y[z[i]] = y[z[i]] - 1
                }
                }
                }
                y_cumsum = cumsum(y)
                rle_object$values <- y_cumsum
                new_vector = inverse.rle(rle_object)
                return(new_vector)
                }

                vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
                vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
                vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2





                share|improve this answer












                I make use of which function in r (https://www.r-bloggers.com/which-function-in-r/) and run length encoding (http://www.cookbook-r.com/Manipulating_data/Finding_sequences_of_identical_values/). Here's my attempt:



                vector_analyse <- function(sample_vector){
                # ----------------------------------------------------------------------------
                # Signature: vector --> vector
                # Author: kon_u
                # Description: Given a sample vector of 0s and 1s, return a sequence of 1s in
                # the data you need to increase the number of children by 1 (when there are less
                # 5 0s in between them, then it is the same child and not a new child)
                # ----------------------------------------------------------------------------

                # ----------------------------------------------------------------------------
                # Run Length Encoding gives a list of length and values
                # ----------------------------------------------------------------------------
                rle_object <- rle(sample_vector)
                x <- rle_object$lengths # original length
                y <- rle_object$values # original values
                z <- which(y == 1) # index of 1 in vector y
                if (length(z) == 1){
                invisible()
                } else{
                for (i in 2:length(z)){
                if (x[z[i]-1] >= 5){
                y[z[i]] = y[z[i]]
                } else {
                y[z[i]] = y[z[i]] - 1
                }
                }
                }
                y_cumsum = cumsum(y)
                rle_object$values <- y_cumsum
                new_vector = inverse.rle(rle_object)
                return(new_vector)
                }

                vector_analyse(c(1,1,1,1,0,0,0,0)) # 1 1 1 1 1 1 1 1
                vector_analyse(c(0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2
                vector_analyse(c(0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1)) # 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 17:42









                kon_u

                1616




                1616






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53247149%2fvector-analysis-in-r%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

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation