replacing special characters in first column only in r












1















Hi I have a text file in this format:



            x  
M.00116 952
M.00046 41483
M.00033 4


I need to replace the "." with an "_" in r. But for I am not able to do it by using this :



sub("\.", "_", c) 


I get this output



c(952, 41483, 4)


I need an out put like :



x  
M-00116 952
M-00046 41483
M-00033 4


What am I doing wrong? Any help is appreciated!










share|improve this question





























    1















    Hi I have a text file in this format:



                x  
    M.00116 952
    M.00046 41483
    M.00033 4


    I need to replace the "." with an "_" in r. But for I am not able to do it by using this :



    sub("\.", "_", c) 


    I get this output



    c(952, 41483, 4)


    I need an out put like :



    x  
    M-00116 952
    M-00046 41483
    M-00033 4


    What am I doing wrong? Any help is appreciated!










    share|improve this question



























      1












      1








      1








      Hi I have a text file in this format:



                  x  
      M.00116 952
      M.00046 41483
      M.00033 4


      I need to replace the "." with an "_" in r. But for I am not able to do it by using this :



      sub("\.", "_", c) 


      I get this output



      c(952, 41483, 4)


      I need an out put like :



      x  
      M-00116 952
      M-00046 41483
      M-00033 4


      What am I doing wrong? Any help is appreciated!










      share|improve this question
















      Hi I have a text file in this format:



                  x  
      M.00116 952
      M.00046 41483
      M.00033 4


      I need to replace the "." with an "_" in r. But for I am not able to do it by using this :



      sub("\.", "_", c) 


      I get this output



      c(952, 41483, 4)


      I need an out put like :



      x  
      M-00116 952
      M-00046 41483
      M-00033 4


      What am I doing wrong? Any help is appreciated!







      r replace






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 16:44









      Jake Kaupp

      5,64721428




      5,64721428










      asked Nov 14 '18 at 16:40









      R_explorerR_explorer

      104




      104
























          2 Answers
          2






          active

          oldest

          votes


















          0














          We can use chartr from base R



          chartr('.', '-', x)
          #[1] "M-00116 952 M-00046 41483 M-00033 4"


          data



          x <- "M.00116 952 M.00046 41483 M.00033 4"





          share|improve this answer
























          • tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

            – R_explorer
            Nov 14 '18 at 16:57













          • @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

            – akrun
            Nov 14 '18 at 16:58






          • 1





            Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

            – R_explorer
            Nov 14 '18 at 17:00



















          0














          Try:



          x <- "M.00116 952 M.00046 41483 M.00033 4"
          gsub("\.", "-", x)


          EDIT:



          Replace "sub" with gsub:



          gsub("\.", "_", data$colname)


          EDIT:



          This worked for me:



          c <- c("M.00116", "M.00046", "M.00033") 
          x <- c("952", "41483", "4")

          d <- cbind(c, x)

          colnames(d)[2] <- ""

          gsub("\.", "_", d)

          c
          [1,] "M_00116" "952"
          [2,] "M_00046" "41483"
          [3,] "M_00033" "4"





          share|improve this answer


























          • I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

            – R_explorer
            Nov 14 '18 at 16:45













          • Are you specifying the column name or using sub over the whole data frame?

            – user113156
            Nov 14 '18 at 16:48











          • tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

            – R_explorer
            Nov 14 '18 at 16:49













          • Change it to: gsub("\.", "-", data$column_name)

            – user113156
            Nov 14 '18 at 16:49











          • I am using it over the whole data frame. the first column has no column name

            – R_explorer
            Nov 14 '18 at 16:49











          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%2f53304957%2freplacing-special-characters-in-first-column-only-in-r%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









          0














          We can use chartr from base R



          chartr('.', '-', x)
          #[1] "M-00116 952 M-00046 41483 M-00033 4"


          data



          x <- "M.00116 952 M.00046 41483 M.00033 4"





          share|improve this answer
























          • tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

            – R_explorer
            Nov 14 '18 at 16:57













          • @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

            – akrun
            Nov 14 '18 at 16:58






          • 1





            Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

            – R_explorer
            Nov 14 '18 at 17:00
















          0














          We can use chartr from base R



          chartr('.', '-', x)
          #[1] "M-00116 952 M-00046 41483 M-00033 4"


          data



          x <- "M.00116 952 M.00046 41483 M.00033 4"





          share|improve this answer
























          • tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

            – R_explorer
            Nov 14 '18 at 16:57













          • @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

            – akrun
            Nov 14 '18 at 16:58






          • 1





            Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

            – R_explorer
            Nov 14 '18 at 17:00














          0












          0








          0







          We can use chartr from base R



          chartr('.', '-', x)
          #[1] "M-00116 952 M-00046 41483 M-00033 4"


          data



          x <- "M.00116 952 M.00046 41483 M.00033 4"





          share|improve this answer













          We can use chartr from base R



          chartr('.', '-', x)
          #[1] "M-00116 952 M-00046 41483 M-00033 4"


          data



          x <- "M.00116 952 M.00046 41483 M.00033 4"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 16:43









          akrunakrun

          408k13198273




          408k13198273













          • tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

            – R_explorer
            Nov 14 '18 at 16:57













          • @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

            – akrun
            Nov 14 '18 at 16:58






          • 1





            Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

            – R_explorer
            Nov 14 '18 at 17:00



















          • tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

            – R_explorer
            Nov 14 '18 at 16:57













          • @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

            – akrun
            Nov 14 '18 at 16:58






          • 1





            Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

            – R_explorer
            Nov 14 '18 at 17:00

















          tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

          – R_explorer
          Nov 14 '18 at 16:57







          tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!

          – R_explorer
          Nov 14 '18 at 16:57















          @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

          – akrun
          Nov 14 '18 at 16:58





          @user2635253 Looks like it is rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))

          – akrun
          Nov 14 '18 at 16:58




          1




          1





          Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

          – R_explorer
          Nov 14 '18 at 17:00





          Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!

          – R_explorer
          Nov 14 '18 at 17:00













          0














          Try:



          x <- "M.00116 952 M.00046 41483 M.00033 4"
          gsub("\.", "-", x)


          EDIT:



          Replace "sub" with gsub:



          gsub("\.", "_", data$colname)


          EDIT:



          This worked for me:



          c <- c("M.00116", "M.00046", "M.00033") 
          x <- c("952", "41483", "4")

          d <- cbind(c, x)

          colnames(d)[2] <- ""

          gsub("\.", "_", d)

          c
          [1,] "M_00116" "952"
          [2,] "M_00046" "41483"
          [3,] "M_00033" "4"





          share|improve this answer


























          • I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

            – R_explorer
            Nov 14 '18 at 16:45













          • Are you specifying the column name or using sub over the whole data frame?

            – user113156
            Nov 14 '18 at 16:48











          • tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

            – R_explorer
            Nov 14 '18 at 16:49













          • Change it to: gsub("\.", "-", data$column_name)

            – user113156
            Nov 14 '18 at 16:49











          • I am using it over the whole data frame. the first column has no column name

            – R_explorer
            Nov 14 '18 at 16:49
















          0














          Try:



          x <- "M.00116 952 M.00046 41483 M.00033 4"
          gsub("\.", "-", x)


          EDIT:



          Replace "sub" with gsub:



          gsub("\.", "_", data$colname)


          EDIT:



          This worked for me:



          c <- c("M.00116", "M.00046", "M.00033") 
          x <- c("952", "41483", "4")

          d <- cbind(c, x)

          colnames(d)[2] <- ""

          gsub("\.", "_", d)

          c
          [1,] "M_00116" "952"
          [2,] "M_00046" "41483"
          [3,] "M_00033" "4"





          share|improve this answer


























          • I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

            – R_explorer
            Nov 14 '18 at 16:45













          • Are you specifying the column name or using sub over the whole data frame?

            – user113156
            Nov 14 '18 at 16:48











          • tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

            – R_explorer
            Nov 14 '18 at 16:49













          • Change it to: gsub("\.", "-", data$column_name)

            – user113156
            Nov 14 '18 at 16:49











          • I am using it over the whole data frame. the first column has no column name

            – R_explorer
            Nov 14 '18 at 16:49














          0












          0








          0







          Try:



          x <- "M.00116 952 M.00046 41483 M.00033 4"
          gsub("\.", "-", x)


          EDIT:



          Replace "sub" with gsub:



          gsub("\.", "_", data$colname)


          EDIT:



          This worked for me:



          c <- c("M.00116", "M.00046", "M.00033") 
          x <- c("952", "41483", "4")

          d <- cbind(c, x)

          colnames(d)[2] <- ""

          gsub("\.", "_", d)

          c
          [1,] "M_00116" "952"
          [2,] "M_00046" "41483"
          [3,] "M_00033" "4"





          share|improve this answer















          Try:



          x <- "M.00116 952 M.00046 41483 M.00033 4"
          gsub("\.", "-", x)


          EDIT:



          Replace "sub" with gsub:



          gsub("\.", "_", data$colname)


          EDIT:



          This worked for me:



          c <- c("M.00116", "M.00046", "M.00033") 
          x <- c("952", "41483", "4")

          d <- cbind(c, x)

          colnames(d)[2] <- ""

          gsub("\.", "_", d)

          c
          [1,] "M_00116" "952"
          [2,] "M_00046" "41483"
          [3,] "M_00033" "4"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 16:55

























          answered Nov 14 '18 at 16:42









          user113156user113156

          8871419




          8871419













          • I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

            – R_explorer
            Nov 14 '18 at 16:45













          • Are you specifying the column name or using sub over the whole data frame?

            – user113156
            Nov 14 '18 at 16:48











          • tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

            – R_explorer
            Nov 14 '18 at 16:49













          • Change it to: gsub("\.", "-", data$column_name)

            – user113156
            Nov 14 '18 at 16:49











          • I am using it over the whole data frame. the first column has no column name

            – R_explorer
            Nov 14 '18 at 16:49



















          • I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

            – R_explorer
            Nov 14 '18 at 16:45













          • Are you specifying the column name or using sub over the whole data frame?

            – user113156
            Nov 14 '18 at 16:48











          • tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

            – R_explorer
            Nov 14 '18 at 16:49













          • Change it to: gsub("\.", "-", data$column_name)

            – user113156
            Nov 14 '18 at 16:49











          • I am using it over the whole data frame. the first column has no column name

            – R_explorer
            Nov 14 '18 at 16:49

















          I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

          – R_explorer
          Nov 14 '18 at 16:45







          I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second

          – R_explorer
          Nov 14 '18 at 16:45















          Are you specifying the column name or using sub over the whole data frame?

          – user113156
          Nov 14 '18 at 16:48





          Are you specifying the column name or using sub over the whole data frame?

          – user113156
          Nov 14 '18 at 16:48













          tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

          – R_explorer
          Nov 14 '18 at 16:49







          tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it

          – R_explorer
          Nov 14 '18 at 16:49















          Change it to: gsub("\.", "-", data$column_name)

          – user113156
          Nov 14 '18 at 16:49





          Change it to: gsub("\.", "-", data$column_name)

          – user113156
          Nov 14 '18 at 16:49













          I am using it over the whole data frame. the first column has no column name

          – R_explorer
          Nov 14 '18 at 16:49





          I am using it over the whole data frame. the first column has no column name

          – R_explorer
          Nov 14 '18 at 16:49


















          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%2f53304957%2freplacing-special-characters-in-first-column-only-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

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly