How to find the smallest values of the vectors sequentially












0















Suppose I have a vector x and I would like to find the smallest values of this vector. Then, I would like to return a new x without the selected element and then extract the smallest value from the remaining values and so on.



For example,



x <- c(3,4,2,1,6,10.14,100,7)


I would like to extract the smallest value of this vector. In this example, the smallest value is 1. Then, I would like to return x without 1.



x_1 <- c(3,4,2,6,10.14,100,7)


After that, I need to select the smallest value from x_1 and then get x_2 and so on.



How can I do this automatically in R?










share|improve this question




















  • 2





    Start with x[-which.min(x)].

    – r.user.05apr
    Nov 23 '18 at 9:32











  • Related: Remove the maximum value of a vector in R. Now you just need to iterate (eg., with while).

    – PoGibas
    Nov 23 '18 at 9:33


















0















Suppose I have a vector x and I would like to find the smallest values of this vector. Then, I would like to return a new x without the selected element and then extract the smallest value from the remaining values and so on.



For example,



x <- c(3,4,2,1,6,10.14,100,7)


I would like to extract the smallest value of this vector. In this example, the smallest value is 1. Then, I would like to return x without 1.



x_1 <- c(3,4,2,6,10.14,100,7)


After that, I need to select the smallest value from x_1 and then get x_2 and so on.



How can I do this automatically in R?










share|improve this question




















  • 2





    Start with x[-which.min(x)].

    – r.user.05apr
    Nov 23 '18 at 9:32











  • Related: Remove the maximum value of a vector in R. Now you just need to iterate (eg., with while).

    – PoGibas
    Nov 23 '18 at 9:33
















0












0








0








Suppose I have a vector x and I would like to find the smallest values of this vector. Then, I would like to return a new x without the selected element and then extract the smallest value from the remaining values and so on.



For example,



x <- c(3,4,2,1,6,10.14,100,7)


I would like to extract the smallest value of this vector. In this example, the smallest value is 1. Then, I would like to return x without 1.



x_1 <- c(3,4,2,6,10.14,100,7)


After that, I need to select the smallest value from x_1 and then get x_2 and so on.



How can I do this automatically in R?










share|improve this question
















Suppose I have a vector x and I would like to find the smallest values of this vector. Then, I would like to return a new x without the selected element and then extract the smallest value from the remaining values and so on.



For example,



x <- c(3,4,2,1,6,10.14,100,7)


I would like to extract the smallest value of this vector. In this example, the smallest value is 1. Then, I would like to return x without 1.



x_1 <- c(3,4,2,6,10.14,100,7)


After that, I need to select the smallest value from x_1 and then get x_2 and so on.



How can I do this automatically in R?







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:51









Ronak Shah

38.3k104161




38.3k104161










asked Nov 23 '18 at 9:28









MaryamMaryam

28911




28911








  • 2





    Start with x[-which.min(x)].

    – r.user.05apr
    Nov 23 '18 at 9:32











  • Related: Remove the maximum value of a vector in R. Now you just need to iterate (eg., with while).

    – PoGibas
    Nov 23 '18 at 9:33
















  • 2





    Start with x[-which.min(x)].

    – r.user.05apr
    Nov 23 '18 at 9:32











  • Related: Remove the maximum value of a vector in R. Now you just need to iterate (eg., with while).

    – PoGibas
    Nov 23 '18 at 9:33










2




2





Start with x[-which.min(x)].

– r.user.05apr
Nov 23 '18 at 9:32





Start with x[-which.min(x)].

– r.user.05apr
Nov 23 '18 at 9:32













Related: Remove the maximum value of a vector in R. Now you just need to iterate (eg., with while).

– PoGibas
Nov 23 '18 at 9:33







Related: Remove the maximum value of a vector in R. Now you just need to iterate (eg., with while).

– PoGibas
Nov 23 '18 at 9:33














1 Answer
1






active

oldest

votes


















4














We can create a function remove_small_vec which removes the lowest value from the vector. We then run a while loop which runs till length of vector is >= 1.



remove_small_vec <- function(vec) {
vec[vec != min(vec)]
}

list_vec <- list()

while (length(x) >= 1) {
list_vec <- c(list_vec, list(x))
x <- remove_small_vec(x)
}


list_vec
#[[1]]
#[1] 3.00 4.00 2.00 1.00 6.00 10.14 100.00 7.00

#[[2]]
#[1] 3.00 4.00 2.00 6.00 10.14 100.00 7.00

#[[3]]
#[1] 3.00 4.00 6.00 10.14 100.00 7.00

#[[4]]
#[1] 4.00 6.00 10.14 100.00 7.00

#[[5]]
#[1] 6.00 10.14 100.00 7.00

#[[6]]
#[1] 10.14 100.00 7.00

#[[7]]
#[1] 10.14 100.00

#[[8]]
#[1] 100


This returns a list of vectors of length same as x with one element removed in each list element.



data



x <- c(3,4,2,1,6,10.14,100,7)





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%2f53443882%2fhow-to-find-the-smallest-values-of-the-vectors-sequentially%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









    4














    We can create a function remove_small_vec which removes the lowest value from the vector. We then run a while loop which runs till length of vector is >= 1.



    remove_small_vec <- function(vec) {
    vec[vec != min(vec)]
    }

    list_vec <- list()

    while (length(x) >= 1) {
    list_vec <- c(list_vec, list(x))
    x <- remove_small_vec(x)
    }


    list_vec
    #[[1]]
    #[1] 3.00 4.00 2.00 1.00 6.00 10.14 100.00 7.00

    #[[2]]
    #[1] 3.00 4.00 2.00 6.00 10.14 100.00 7.00

    #[[3]]
    #[1] 3.00 4.00 6.00 10.14 100.00 7.00

    #[[4]]
    #[1] 4.00 6.00 10.14 100.00 7.00

    #[[5]]
    #[1] 6.00 10.14 100.00 7.00

    #[[6]]
    #[1] 10.14 100.00 7.00

    #[[7]]
    #[1] 10.14 100.00

    #[[8]]
    #[1] 100


    This returns a list of vectors of length same as x with one element removed in each list element.



    data



    x <- c(3,4,2,1,6,10.14,100,7)





    share|improve this answer




























      4














      We can create a function remove_small_vec which removes the lowest value from the vector. We then run a while loop which runs till length of vector is >= 1.



      remove_small_vec <- function(vec) {
      vec[vec != min(vec)]
      }

      list_vec <- list()

      while (length(x) >= 1) {
      list_vec <- c(list_vec, list(x))
      x <- remove_small_vec(x)
      }


      list_vec
      #[[1]]
      #[1] 3.00 4.00 2.00 1.00 6.00 10.14 100.00 7.00

      #[[2]]
      #[1] 3.00 4.00 2.00 6.00 10.14 100.00 7.00

      #[[3]]
      #[1] 3.00 4.00 6.00 10.14 100.00 7.00

      #[[4]]
      #[1] 4.00 6.00 10.14 100.00 7.00

      #[[5]]
      #[1] 6.00 10.14 100.00 7.00

      #[[6]]
      #[1] 10.14 100.00 7.00

      #[[7]]
      #[1] 10.14 100.00

      #[[8]]
      #[1] 100


      This returns a list of vectors of length same as x with one element removed in each list element.



      data



      x <- c(3,4,2,1,6,10.14,100,7)





      share|improve this answer


























        4












        4








        4







        We can create a function remove_small_vec which removes the lowest value from the vector. We then run a while loop which runs till length of vector is >= 1.



        remove_small_vec <- function(vec) {
        vec[vec != min(vec)]
        }

        list_vec <- list()

        while (length(x) >= 1) {
        list_vec <- c(list_vec, list(x))
        x <- remove_small_vec(x)
        }


        list_vec
        #[[1]]
        #[1] 3.00 4.00 2.00 1.00 6.00 10.14 100.00 7.00

        #[[2]]
        #[1] 3.00 4.00 2.00 6.00 10.14 100.00 7.00

        #[[3]]
        #[1] 3.00 4.00 6.00 10.14 100.00 7.00

        #[[4]]
        #[1] 4.00 6.00 10.14 100.00 7.00

        #[[5]]
        #[1] 6.00 10.14 100.00 7.00

        #[[6]]
        #[1] 10.14 100.00 7.00

        #[[7]]
        #[1] 10.14 100.00

        #[[8]]
        #[1] 100


        This returns a list of vectors of length same as x with one element removed in each list element.



        data



        x <- c(3,4,2,1,6,10.14,100,7)





        share|improve this answer













        We can create a function remove_small_vec which removes the lowest value from the vector. We then run a while loop which runs till length of vector is >= 1.



        remove_small_vec <- function(vec) {
        vec[vec != min(vec)]
        }

        list_vec <- list()

        while (length(x) >= 1) {
        list_vec <- c(list_vec, list(x))
        x <- remove_small_vec(x)
        }


        list_vec
        #[[1]]
        #[1] 3.00 4.00 2.00 1.00 6.00 10.14 100.00 7.00

        #[[2]]
        #[1] 3.00 4.00 2.00 6.00 10.14 100.00 7.00

        #[[3]]
        #[1] 3.00 4.00 6.00 10.14 100.00 7.00

        #[[4]]
        #[1] 4.00 6.00 10.14 100.00 7.00

        #[[5]]
        #[1] 6.00 10.14 100.00 7.00

        #[[6]]
        #[1] 10.14 100.00 7.00

        #[[7]]
        #[1] 10.14 100.00

        #[[8]]
        #[1] 100


        This returns a list of vectors of length same as x with one element removed in each list element.



        data



        x <- c(3,4,2,1,6,10.14,100,7)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 9:38









        Ronak ShahRonak Shah

        38.3k104161




        38.3k104161
































            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%2f53443882%2fhow-to-find-the-smallest-values-of-the-vectors-sequentially%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin