How to find and replace min value in dataframe with text in r












0















i have dataframe with 20 columns and I like to identify the minimum value in each of the column and replace them with text such as "min". Appreciate any help



sample data :



   a    b     c
-0.05 0.31 0.62
0.78 0.25 -0.01
0.68 0.33 -0.04
-0.01 0.30 0.56
0.55 0.28 -0.03


Desired output



    a    b     c
min 0.31 0.62
0.78 min -0.01
0.68 0.33 min
-0.01 0.30 0.56
0.55 0.28 -0.03









share|improve this question





























    0















    i have dataframe with 20 columns and I like to identify the minimum value in each of the column and replace them with text such as "min". Appreciate any help



    sample data :



       a    b     c
    -0.05 0.31 0.62
    0.78 0.25 -0.01
    0.68 0.33 -0.04
    -0.01 0.30 0.56
    0.55 0.28 -0.03


    Desired output



        a    b     c
    min 0.31 0.62
    0.78 min -0.01
    0.68 0.33 min
    -0.01 0.30 0.56
    0.55 0.28 -0.03









    share|improve this question



























      0












      0








      0








      i have dataframe with 20 columns and I like to identify the minimum value in each of the column and replace them with text such as "min". Appreciate any help



      sample data :



         a    b     c
      -0.05 0.31 0.62
      0.78 0.25 -0.01
      0.68 0.33 -0.04
      -0.01 0.30 0.56
      0.55 0.28 -0.03


      Desired output



          a    b     c
      min 0.31 0.62
      0.78 min -0.01
      0.68 0.33 min
      -0.01 0.30 0.56
      0.55 0.28 -0.03









      share|improve this question
















      i have dataframe with 20 columns and I like to identify the minimum value in each of the column and replace them with text such as "min". Appreciate any help



      sample data :



         a    b     c
      -0.05 0.31 0.62
      0.78 0.25 -0.01
      0.68 0.33 -0.04
      -0.01 0.30 0.56
      0.55 0.28 -0.03


      Desired output



          a    b     c
      min 0.31 0.62
      0.78 min -0.01
      0.68 0.33 min
      -0.01 0.30 0.56
      0.55 0.28 -0.03






      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 20:29









      markus

      11.1k1031




      11.1k1031










      asked Nov 14 '18 at 20:21









      gurtejgurtej

      83




      83
























          3 Answers
          3






          active

          oldest

          votes


















          1














          apply(df, MARGIN=2, FUN=(function(x){x[which.min(x)] <- 'min'; return(x)})





          share|improve this answer































            1














            You can apply a function to each column that replaces the minimum value with a string. This returns a matrix which could be converted into a data frame if desired. As IceCreamToucan pointed out, all rows will be of type character since each variable must have the same type:



            apply(df, 2, function(x) {
            x[x == min(x)] <- 'min'
            return(x)
            })

            a b c
            [1,] "min" "0.31" "0.62"
            [2,] "0.78" "min" "-0.01"
            [3,] "0.68" "0.33" "min"
            [4,] "-0.01" "0.3" "0.56"
            [5,] "0.55" "0.28" "-0.03"





            share|improve this answer


























            • thank you very much

              – gurtej
              Nov 14 '18 at 20:30






            • 1





              It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

              – markus
              Nov 14 '18 at 20:32



















            1














            You can use the method below, but know that this converts all your columns to character, since vectors must have elements which all have the same type.



            library(dplyr)

            df %>%
            mutate_all(~ replace(.x, which.min(.x), 'min'))
            # a b c
            # 1 min 0.31 0.62
            # 2 0.78 min -0.01
            # 3 0.68 0.33 min
            # 4 -0.01 0.3 0.56
            # 5 0.55 0.28 -0.03





            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%2f53308173%2fhow-to-find-and-replace-min-value-in-dataframe-with-text-in-r%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              apply(df, MARGIN=2, FUN=(function(x){x[which.min(x)] <- 'min'; return(x)})





              share|improve this answer




























                1














                apply(df, MARGIN=2, FUN=(function(x){x[which.min(x)] <- 'min'; return(x)})





                share|improve this answer


























                  1












                  1








                  1







                  apply(df, MARGIN=2, FUN=(function(x){x[which.min(x)] <- 'min'; return(x)})





                  share|improve this answer













                  apply(df, MARGIN=2, FUN=(function(x){x[which.min(x)] <- 'min'; return(x)})






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 20:30









                  emsinkoemsinko

                  17115




                  17115

























                      1














                      You can apply a function to each column that replaces the minimum value with a string. This returns a matrix which could be converted into a data frame if desired. As IceCreamToucan pointed out, all rows will be of type character since each variable must have the same type:



                      apply(df, 2, function(x) {
                      x[x == min(x)] <- 'min'
                      return(x)
                      })

                      a b c
                      [1,] "min" "0.31" "0.62"
                      [2,] "0.78" "min" "-0.01"
                      [3,] "0.68" "0.33" "min"
                      [4,] "-0.01" "0.3" "0.56"
                      [5,] "0.55" "0.28" "-0.03"





                      share|improve this answer


























                      • thank you very much

                        – gurtej
                        Nov 14 '18 at 20:30






                      • 1





                        It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

                        – markus
                        Nov 14 '18 at 20:32
















                      1














                      You can apply a function to each column that replaces the minimum value with a string. This returns a matrix which could be converted into a data frame if desired. As IceCreamToucan pointed out, all rows will be of type character since each variable must have the same type:



                      apply(df, 2, function(x) {
                      x[x == min(x)] <- 'min'
                      return(x)
                      })

                      a b c
                      [1,] "min" "0.31" "0.62"
                      [2,] "0.78" "min" "-0.01"
                      [3,] "0.68" "0.33" "min"
                      [4,] "-0.01" "0.3" "0.56"
                      [5,] "0.55" "0.28" "-0.03"





                      share|improve this answer


























                      • thank you very much

                        – gurtej
                        Nov 14 '18 at 20:30






                      • 1





                        It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

                        – markus
                        Nov 14 '18 at 20:32














                      1












                      1








                      1







                      You can apply a function to each column that replaces the minimum value with a string. This returns a matrix which could be converted into a data frame if desired. As IceCreamToucan pointed out, all rows will be of type character since each variable must have the same type:



                      apply(df, 2, function(x) {
                      x[x == min(x)] <- 'min'
                      return(x)
                      })

                      a b c
                      [1,] "min" "0.31" "0.62"
                      [2,] "0.78" "min" "-0.01"
                      [3,] "0.68" "0.33" "min"
                      [4,] "-0.01" "0.3" "0.56"
                      [5,] "0.55" "0.28" "-0.03"





                      share|improve this answer















                      You can apply a function to each column that replaces the minimum value with a string. This returns a matrix which could be converted into a data frame if desired. As IceCreamToucan pointed out, all rows will be of type character since each variable must have the same type:



                      apply(df, 2, function(x) {
                      x[x == min(x)] <- 'min'
                      return(x)
                      })

                      a b c
                      [1,] "min" "0.31" "0.62"
                      [2,] "0.78" "min" "-0.01"
                      [3,] "0.68" "0.33" "min"
                      [4,] "-0.01" "0.3" "0.56"
                      [5,] "0.55" "0.28" "-0.03"






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 14 '18 at 20:43

























                      answered Nov 14 '18 at 20:28









                      divibisandivibisan

                      4,49381531




                      4,49381531













                      • thank you very much

                        – gurtej
                        Nov 14 '18 at 20:30






                      • 1





                        It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

                        – markus
                        Nov 14 '18 at 20:32



















                      • thank you very much

                        – gurtej
                        Nov 14 '18 at 20:30






                      • 1





                        It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

                        – markus
                        Nov 14 '18 at 20:32

















                      thank you very much

                      – gurtej
                      Nov 14 '18 at 20:30





                      thank you very much

                      – gurtej
                      Nov 14 '18 at 20:30




                      1




                      1





                      It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

                      – markus
                      Nov 14 '18 at 20:32





                      It might be better to do df <- lapply(df, function(x) replace(x, x == min(x), "min")) to get a data frame in return.

                      – markus
                      Nov 14 '18 at 20:32











                      1














                      You can use the method below, but know that this converts all your columns to character, since vectors must have elements which all have the same type.



                      library(dplyr)

                      df %>%
                      mutate_all(~ replace(.x, which.min(.x), 'min'))
                      # a b c
                      # 1 min 0.31 0.62
                      # 2 0.78 min -0.01
                      # 3 0.68 0.33 min
                      # 4 -0.01 0.3 0.56
                      # 5 0.55 0.28 -0.03





                      share|improve this answer






























                        1














                        You can use the method below, but know that this converts all your columns to character, since vectors must have elements which all have the same type.



                        library(dplyr)

                        df %>%
                        mutate_all(~ replace(.x, which.min(.x), 'min'))
                        # a b c
                        # 1 min 0.31 0.62
                        # 2 0.78 min -0.01
                        # 3 0.68 0.33 min
                        # 4 -0.01 0.3 0.56
                        # 5 0.55 0.28 -0.03





                        share|improve this answer




























                          1












                          1








                          1







                          You can use the method below, but know that this converts all your columns to character, since vectors must have elements which all have the same type.



                          library(dplyr)

                          df %>%
                          mutate_all(~ replace(.x, which.min(.x), 'min'))
                          # a b c
                          # 1 min 0.31 0.62
                          # 2 0.78 min -0.01
                          # 3 0.68 0.33 min
                          # 4 -0.01 0.3 0.56
                          # 5 0.55 0.28 -0.03





                          share|improve this answer















                          You can use the method below, but know that this converts all your columns to character, since vectors must have elements which all have the same type.



                          library(dplyr)

                          df %>%
                          mutate_all(~ replace(.x, which.min(.x), 'min'))
                          # a b c
                          # 1 min 0.31 0.62
                          # 2 0.78 min -0.01
                          # 3 0.68 0.33 min
                          # 4 -0.01 0.3 0.56
                          # 5 0.55 0.28 -0.03






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 14 '18 at 21:06

























                          answered Nov 14 '18 at 20:26









                          IceCreamToucanIceCreamToucan

                          9,2611716




                          9,2611716






























                              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%2f53308173%2fhow-to-find-and-replace-min-value-in-dataframe-with-text-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







                              這個網誌中的熱門文章

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud

                              Zucchini