how to I replace numeric values with a string in an R dataframe?












0












$begingroup$


I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.



df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')


Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.



Also tried



df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')


Any pointers highly appreciated.










share|improve this question









$endgroup$




bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    $begingroup$
    Do you have a dummy dataframe that you can provide?
    $endgroup$
    – n1k31t4
    Sep 19 '18 at 19:40










  • $begingroup$
    df[is.numeric(df)]="string".
    $endgroup$
    – user2974951
    Sep 20 '18 at 6:31
















0












$begingroup$


I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.



df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')


Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.



Also tried



df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')


Any pointers highly appreciated.










share|improve this question









$endgroup$




bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    $begingroup$
    Do you have a dummy dataframe that you can provide?
    $endgroup$
    – n1k31t4
    Sep 19 '18 at 19:40










  • $begingroup$
    df[is.numeric(df)]="string".
    $endgroup$
    – user2974951
    Sep 20 '18 at 6:31














0












0








0





$begingroup$


I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.



df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')


Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.



Also tried



df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')


Any pointers highly appreciated.










share|improve this question









$endgroup$




I want to replace all numeric values in a column in my data frame with a string value. The following doesn't seem to work.



df <- within(df, myCol[is.numeric(myCol)] <- 'NOTMISSING')


Even though the df has some values as NA and others as numbers, all values are being replaced with NOTMISSING.



Also tried



df <- within(df, myCol[is_numeric(myCol)] <- 'NOTMISSING')


Any pointers highly appreciated.







r dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 19 '18 at 16:31









ramsrams

1011




1011





bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    $begingroup$
    Do you have a dummy dataframe that you can provide?
    $endgroup$
    – n1k31t4
    Sep 19 '18 at 19:40










  • $begingroup$
    df[is.numeric(df)]="string".
    $endgroup$
    – user2974951
    Sep 20 '18 at 6:31














  • 1




    $begingroup$
    Do you have a dummy dataframe that you can provide?
    $endgroup$
    – n1k31t4
    Sep 19 '18 at 19:40










  • $begingroup$
    df[is.numeric(df)]="string".
    $endgroup$
    – user2974951
    Sep 20 '18 at 6:31








1




1




$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40




$begingroup$
Do you have a dummy dataframe that you can provide?
$endgroup$
– n1k31t4
Sep 19 '18 at 19:40












$begingroup$
df[is.numeric(df)]="string".
$endgroup$
– user2974951
Sep 20 '18 at 6:31




$begingroup$
df[is.numeric(df)]="string".
$endgroup$
– user2974951
Sep 20 '18 at 6:31










2 Answers
2






active

oldest

votes


















1












$begingroup$

From the documentation of is.numeric:




The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.




So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.



is.numeric(c(5, 4, 3))
[1] TRUE

is.numeric(c(5, 4, NA))
[1] TRUE


That's why either all or none of the values are changed to NOTMISSING.



@eg-r's fix is correct. Here's a tidyverse way to accomplish the same.



> df<-tibble(myCol=c(5, 4, NA))
> df
# A tibble: 3 x 1
myCol
<dbl>
1 5
2 4
3 NA
> df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
# A tibble: 3 x 1
myCol
<chr>
1 NOTMISSING
2 NOTMISSING
3 <NA>





share|improve this answer









$endgroup$





















    0












    $begingroup$

    NAs can be numeric (especially if the other values in that column are all numeric). Try this :



    df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)


    Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :



    df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')





    share|improve this answer









    $endgroup$














      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "557"
      };
      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: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fdatascience.stackexchange.com%2fquestions%2f38497%2fhow-to-i-replace-numeric-values-with-a-string-in-an-r-dataframe%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









      1












      $begingroup$

      From the documentation of is.numeric:




      The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.




      So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.



      is.numeric(c(5, 4, 3))
      [1] TRUE

      is.numeric(c(5, 4, NA))
      [1] TRUE


      That's why either all or none of the values are changed to NOTMISSING.



      @eg-r's fix is correct. Here's a tidyverse way to accomplish the same.



      > df<-tibble(myCol=c(5, 4, NA))
      > df
      # A tibble: 3 x 1
      myCol
      <dbl>
      1 5
      2 4
      3 NA
      > df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
      # A tibble: 3 x 1
      myCol
      <chr>
      1 NOTMISSING
      2 NOTMISSING
      3 <NA>





      share|improve this answer









      $endgroup$


















        1












        $begingroup$

        From the documentation of is.numeric:




        The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.




        So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.



        is.numeric(c(5, 4, 3))
        [1] TRUE

        is.numeric(c(5, 4, NA))
        [1] TRUE


        That's why either all or none of the values are changed to NOTMISSING.



        @eg-r's fix is correct. Here's a tidyverse way to accomplish the same.



        > df<-tibble(myCol=c(5, 4, NA))
        > df
        # A tibble: 3 x 1
        myCol
        <dbl>
        1 5
        2 4
        3 NA
        > df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
        # A tibble: 3 x 1
        myCol
        <chr>
        1 NOTMISSING
        2 NOTMISSING
        3 <NA>





        share|improve this answer









        $endgroup$
















          1












          1








          1





          $begingroup$

          From the documentation of is.numeric:




          The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.




          So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.



          is.numeric(c(5, 4, 3))
          [1] TRUE

          is.numeric(c(5, 4, NA))
          [1] TRUE


          That's why either all or none of the values are changed to NOTMISSING.



          @eg-r's fix is correct. Here's a tidyverse way to accomplish the same.



          > df<-tibble(myCol=c(5, 4, NA))
          > df
          # A tibble: 3 x 1
          myCol
          <dbl>
          1 5
          2 4
          3 NA
          > df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
          # A tibble: 3 x 1
          myCol
          <chr>
          1 NOTMISSING
          2 NOTMISSING
          3 <NA>





          share|improve this answer









          $endgroup$



          From the documentation of is.numeric:




          The default method for is.numeric returns TRUE if its argument is of mode "numeric" (type > "double" or type "integer") and not a factor, and FALSE otherwise.




          So for a vector, is.numeric returns a single TRUE, it doesn't test each element as you might expect.



          is.numeric(c(5, 4, 3))
          [1] TRUE

          is.numeric(c(5, 4, NA))
          [1] TRUE


          That's why either all or none of the values are changed to NOTMISSING.



          @eg-r's fix is correct. Here's a tidyverse way to accomplish the same.



          > df<-tibble(myCol=c(5, 4, NA))
          > df
          # A tibble: 3 x 1
          myCol
          <dbl>
          1 5
          2 4
          3 NA
          > df %>% mutate(myCol = ifelse(is.na(myCol), myCol, "NOTMISSING"))
          # A tibble: 3 x 1
          myCol
          <chr>
          1 NOTMISSING
          2 NOTMISSING
          3 <NA>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 17:18









          John RauserJohn Rauser

          1014




          1014























              0












              $begingroup$

              NAs can be numeric (especially if the other values in that column are all numeric). Try this :



              df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)


              Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :



              df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')





              share|improve this answer









              $endgroup$


















                0












                $begingroup$

                NAs can be numeric (especially if the other values in that column are all numeric). Try this :



                df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)


                Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :



                df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')





                share|improve this answer









                $endgroup$
















                  0












                  0








                  0





                  $begingroup$

                  NAs can be numeric (especially if the other values in that column are all numeric). Try this :



                  df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)


                  Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :



                  df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')





                  share|improve this answer









                  $endgroup$



                  NAs can be numeric (especially if the other values in that column are all numeric). Try this :



                  df$myCol = ifelse(is.numeric(df$myCol) & !is.na(df$myCol), "NOTMISSING", df$myCol)


                  Or if all you want to do is turn all values in that column that are not NA as that string, you can change your original code to :



                  df <- within(df, myCol[!is.na(myCol)] <- 'NOTMISSING')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 22 '18 at 0:40









                  eg-reg-r

                  16614




                  16614






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Data Science Stack Exchange!


                      • 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.


                      Use MathJax to format equations. MathJax reference.


                      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%2fdatascience.stackexchange.com%2fquestions%2f38497%2fhow-to-i-replace-numeric-values-with-a-string-in-an-r-dataframe%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

                      Callistus I

                      Tabula Rosettana

                      How to label and detect the document text images