How to add Double quotes and comma to a column in R dataframe?












0















I have a dataframe with a single column. I want all values in the column to be wrapped in a double quote and comma like below.



df <- data.frame("SN" = 1:4, "Name" = c("John", "Dora","Peter","Lilly"), stringsAsFactors = FALSE)

df$Name


When I extract the Name column, I want it df$Name as



"John",
"Dora",
"Peter",
"Lilly"


I tried paste, gsub and shQuote but it doesn't give me the result that I want. Any help is appreciated.










share|improve this question





























    0















    I have a dataframe with a single column. I want all values in the column to be wrapped in a double quote and comma like below.



    df <- data.frame("SN" = 1:4, "Name" = c("John", "Dora","Peter","Lilly"), stringsAsFactors = FALSE)

    df$Name


    When I extract the Name column, I want it df$Name as



    "John",
    "Dora",
    "Peter",
    "Lilly"


    I tried paste, gsub and shQuote but it doesn't give me the result that I want. Any help is appreciated.










    share|improve this question



























      0












      0








      0


      1






      I have a dataframe with a single column. I want all values in the column to be wrapped in a double quote and comma like below.



      df <- data.frame("SN" = 1:4, "Name" = c("John", "Dora","Peter","Lilly"), stringsAsFactors = FALSE)

      df$Name


      When I extract the Name column, I want it df$Name as



      "John",
      "Dora",
      "Peter",
      "Lilly"


      I tried paste, gsub and shQuote but it doesn't give me the result that I want. Any help is appreciated.










      share|improve this question
















      I have a dataframe with a single column. I want all values in the column to be wrapped in a double quote and comma like below.



      df <- data.frame("SN" = 1:4, "Name" = c("John", "Dora","Peter","Lilly"), stringsAsFactors = FALSE)

      df$Name


      When I extract the Name column, I want it df$Name as



      "John",
      "Dora",
      "Peter",
      "Lilly"


      I tried paste, gsub and shQuote but it doesn't give me the result that I want. Any help is appreciated.







      r dataframe paste gsub double-quotes






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 2:33









      hrbrmstr

      60.8k688150




      60.8k688150










      asked Nov 14 '18 at 2:28









      DatamaniacDatamaniac

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          2














          data.frame(
          SN = 1:4,
          Name = c("John", "Dora", "Peter", "Lilly"),
          stringsAsFactors = FALSE
          ) -> xdf


          sprintf():



          sprintf('"%s",', xdf$Name)
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sprintf('"%s",', xdf$Name), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          paste():



          paste('"', xdf$Name, '",', sep = "")
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(paste('"', xdf$Name, '",', sep = ""), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          sub():



          sub("$", '",', sub("^", '"', xdf$Name))
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sub("$", '",', sub("^", '"', xdf$Name)), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          FWIW sprintf() wins:



          microbenchmark::microbenchmark(
          sprintf = sprintf('"%s",', xdf$Name),
          paste = paste('"', xdf$Name, '",', sep = ""),
          sub = sub("$", '",', sub("^", '"', xdf$Name))
          )
          ## Unit: microseconds
          ## expr min lq mean median uq max neval
          ## sprintf 11.705 13.4955 25.91594 19.6095 32.1780 170.989 100
          ## paste 13.133 15.3150 34.31982 19.0455 31.9615 340.482 100
          ## sub 31.271 34.8555 64.41124 39.8840 78.0180 533.065 100


          UPDATE



          I kinda figured you were aiming for:



          paste0(sprintf('"%s"', xdf$Name), collapse = ", ")
          ## [1] ""John", "Dora", "Peter", "Lilly""

          cat(paste0(sprintf('"%s"', xdf$Name), collapse = ", "))
          ## "John", "Dora", "Peter", "Lilly"





          share|improve this answer


























          • Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

            – Datamaniac
            Nov 14 '18 at 2:46











          • I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

            – hrbrmstr
            Nov 14 '18 at 2:47













          • The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

            – Datamaniac
            Nov 14 '18 at 3:04











          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%2f53292343%2fhow-to-add-double-quotes-and-comma-to-a-column-in-r-dataframe%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









          2














          data.frame(
          SN = 1:4,
          Name = c("John", "Dora", "Peter", "Lilly"),
          stringsAsFactors = FALSE
          ) -> xdf


          sprintf():



          sprintf('"%s",', xdf$Name)
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sprintf('"%s",', xdf$Name), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          paste():



          paste('"', xdf$Name, '",', sep = "")
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(paste('"', xdf$Name, '",', sep = ""), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          sub():



          sub("$", '",', sub("^", '"', xdf$Name))
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sub("$", '",', sub("^", '"', xdf$Name)), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          FWIW sprintf() wins:



          microbenchmark::microbenchmark(
          sprintf = sprintf('"%s",', xdf$Name),
          paste = paste('"', xdf$Name, '",', sep = ""),
          sub = sub("$", '",', sub("^", '"', xdf$Name))
          )
          ## Unit: microseconds
          ## expr min lq mean median uq max neval
          ## sprintf 11.705 13.4955 25.91594 19.6095 32.1780 170.989 100
          ## paste 13.133 15.3150 34.31982 19.0455 31.9615 340.482 100
          ## sub 31.271 34.8555 64.41124 39.8840 78.0180 533.065 100


          UPDATE



          I kinda figured you were aiming for:



          paste0(sprintf('"%s"', xdf$Name), collapse = ", ")
          ## [1] ""John", "Dora", "Peter", "Lilly""

          cat(paste0(sprintf('"%s"', xdf$Name), collapse = ", "))
          ## "John", "Dora", "Peter", "Lilly"





          share|improve this answer


























          • Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

            – Datamaniac
            Nov 14 '18 at 2:46











          • I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

            – hrbrmstr
            Nov 14 '18 at 2:47













          • The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

            – Datamaniac
            Nov 14 '18 at 3:04
















          2














          data.frame(
          SN = 1:4,
          Name = c("John", "Dora", "Peter", "Lilly"),
          stringsAsFactors = FALSE
          ) -> xdf


          sprintf():



          sprintf('"%s",', xdf$Name)
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sprintf('"%s",', xdf$Name), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          paste():



          paste('"', xdf$Name, '",', sep = "")
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(paste('"', xdf$Name, '",', sep = ""), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          sub():



          sub("$", '",', sub("^", '"', xdf$Name))
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sub("$", '",', sub("^", '"', xdf$Name)), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          FWIW sprintf() wins:



          microbenchmark::microbenchmark(
          sprintf = sprintf('"%s",', xdf$Name),
          paste = paste('"', xdf$Name, '",', sep = ""),
          sub = sub("$", '",', sub("^", '"', xdf$Name))
          )
          ## Unit: microseconds
          ## expr min lq mean median uq max neval
          ## sprintf 11.705 13.4955 25.91594 19.6095 32.1780 170.989 100
          ## paste 13.133 15.3150 34.31982 19.0455 31.9615 340.482 100
          ## sub 31.271 34.8555 64.41124 39.8840 78.0180 533.065 100


          UPDATE



          I kinda figured you were aiming for:



          paste0(sprintf('"%s"', xdf$Name), collapse = ", ")
          ## [1] ""John", "Dora", "Peter", "Lilly""

          cat(paste0(sprintf('"%s"', xdf$Name), collapse = ", "))
          ## "John", "Dora", "Peter", "Lilly"





          share|improve this answer


























          • Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

            – Datamaniac
            Nov 14 '18 at 2:46











          • I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

            – hrbrmstr
            Nov 14 '18 at 2:47













          • The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

            – Datamaniac
            Nov 14 '18 at 3:04














          2












          2








          2







          data.frame(
          SN = 1:4,
          Name = c("John", "Dora", "Peter", "Lilly"),
          stringsAsFactors = FALSE
          ) -> xdf


          sprintf():



          sprintf('"%s",', xdf$Name)
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sprintf('"%s",', xdf$Name), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          paste():



          paste('"', xdf$Name, '",', sep = "")
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(paste('"', xdf$Name, '",', sep = ""), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          sub():



          sub("$", '",', sub("^", '"', xdf$Name))
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sub("$", '",', sub("^", '"', xdf$Name)), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          FWIW sprintf() wins:



          microbenchmark::microbenchmark(
          sprintf = sprintf('"%s",', xdf$Name),
          paste = paste('"', xdf$Name, '",', sep = ""),
          sub = sub("$", '",', sub("^", '"', xdf$Name))
          )
          ## Unit: microseconds
          ## expr min lq mean median uq max neval
          ## sprintf 11.705 13.4955 25.91594 19.6095 32.1780 170.989 100
          ## paste 13.133 15.3150 34.31982 19.0455 31.9615 340.482 100
          ## sub 31.271 34.8555 64.41124 39.8840 78.0180 533.065 100


          UPDATE



          I kinda figured you were aiming for:



          paste0(sprintf('"%s"', xdf$Name), collapse = ", ")
          ## [1] ""John", "Dora", "Peter", "Lilly""

          cat(paste0(sprintf('"%s"', xdf$Name), collapse = ", "))
          ## "John", "Dora", "Peter", "Lilly"





          share|improve this answer















          data.frame(
          SN = 1:4,
          Name = c("John", "Dora", "Peter", "Lilly"),
          stringsAsFactors = FALSE
          ) -> xdf


          sprintf():



          sprintf('"%s",', xdf$Name)
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sprintf('"%s",', xdf$Name), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          paste():



          paste('"', xdf$Name, '",', sep = "")
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(paste('"', xdf$Name, '",', sep = ""), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          sub():



          sub("$", '",', sub("^", '"', xdf$Name))
          ## [1] ""John"," ""Dora"," ""Peter"," ""Lilly","

          cat(sub("$", '",', sub("^", '"', xdf$Name)), sep="n")
          ## "John",
          ## "Dora",
          ## "Peter",
          ## "Lilly",


          FWIW sprintf() wins:



          microbenchmark::microbenchmark(
          sprintf = sprintf('"%s",', xdf$Name),
          paste = paste('"', xdf$Name, '",', sep = ""),
          sub = sub("$", '",', sub("^", '"', xdf$Name))
          )
          ## Unit: microseconds
          ## expr min lq mean median uq max neval
          ## sprintf 11.705 13.4955 25.91594 19.6095 32.1780 170.989 100
          ## paste 13.133 15.3150 34.31982 19.0455 31.9615 340.482 100
          ## sub 31.271 34.8555 64.41124 39.8840 78.0180 533.065 100


          UPDATE



          I kinda figured you were aiming for:



          paste0(sprintf('"%s"', xdf$Name), collapse = ", ")
          ## [1] ""John", "Dora", "Peter", "Lilly""

          cat(paste0(sprintf('"%s"', xdf$Name), collapse = ", "))
          ## "John", "Dora", "Peter", "Lilly"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 2:48

























          answered Nov 14 '18 at 2:36









          hrbrmstrhrbrmstr

          60.8k688150




          60.8k688150













          • Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

            – Datamaniac
            Nov 14 '18 at 2:46











          • I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

            – hrbrmstr
            Nov 14 '18 at 2:47













          • The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

            – Datamaniac
            Nov 14 '18 at 3:04



















          • Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

            – Datamaniac
            Nov 14 '18 at 2:46











          • I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

            – hrbrmstr
            Nov 14 '18 at 2:47













          • The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

            – Datamaniac
            Nov 14 '18 at 3:04

















          Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

          – Datamaniac
          Nov 14 '18 at 2:46





          Thanks much. If I have to remove the comma from last value of Name column, how do I do that ?

          – Datamaniac
          Nov 14 '18 at 2:46













          I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

          – hrbrmstr
          Nov 14 '18 at 2:47







          I figured you had an actual task at hand. Please add all your requirements and final desired output to the actual question vs get to it piecemeal, but check the update.

          – hrbrmstr
          Nov 14 '18 at 2:47















          The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

          – Datamaniac
          Nov 14 '18 at 3:04





          The update you posted gives me values instead of a dataframe with a single column. When I use as.data.frame, its an empty list

          – Datamaniac
          Nov 14 '18 at 3:04


















          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%2f53292343%2fhow-to-add-double-quotes-and-comma-to-a-column-in-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

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values