[R] Putting commas in between character strings

William Dunlap wdunlap at tibco.com
Tue Jun 14 21:19:39 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Abraham Mathew
> Sent: Tuesday, June 14, 2011 9:36 AM
> To: r-help at r-project.org
> Subject: [R] Putting commas in between character strings
> 
> I have a number of strings in a vector, and want the output 
> to be seperated
> by commas.
> 
> > t  [1] "35004" "35005" "35006" "35007" "35010" "35014" "35016"
> 
> So I want want it to look like:
> 
> "35004", 35005", "35006", "35007",...

You need to distinguish between printed output and
the return value of a function.  If you want to convert
the vector made by
  x <- sprintf("%5d", 35004:35050)
to the string of that format you can use
  paste(sprintf("\"%s\"", x), collapse=", ")
Its printed output will not look like what you want
because the print routine adds the [1] and escapes
the quotes, etc.

To print x as you showed try something like
  > cat(strwrap(paste(sprintf("\"%s\"", x), collapse=", "), width=40),
sep="\n")
  "35004", "35005", "35006", "35007",
  "35008", "35009", "35010", "35011",
  "35012", "35013", "35014", "35015",
  "35016", "35017", "35018", "35019",
  "35020", "35021", "35022", "35023",
  "35024", "35025", "35026", "35027",
  "35028", "35029", "35030", "35031",
  "35032", "35033", "35034", "35035",
  "35036", "35037", "35038", "35039",
  "35040", "35041", "35042", "35043",
  "35044", "35045", "35046", "35047",
  "35048", "35049", "35050"
If you use that format a bunch, write a function
to encapsulate the usage
  printWithQuotesCommasAndWrapped <- function(x) {
     cat(strwrap(paste(sprintf("\"%s\"", x), collapse=", "), width=40),
sep="\n")
  }
and use it as
  > printWithQuotesCommasAndWrapped(x)
If that sort of object should always be printed that
way, make function to add a class to the object and
a print method for that class that calls printW...d:
  qcw <- function(x) {
    class(x) <- c("WithQuotesCommasAndWrapped", class(x))
    x
  }
  print.WithQuotesCommasAndWrapped <- function(x, ...) {
    printWithQuotesCommasAndWrapped(x)
  }
and use it as
  > list(head=qcw(x[1:8]), middle=qcw(x[15:20]))
  $head
  "35004", "35005", "35006", "35007",
  "35008", "35009", "35010", "35011"

  $middle
  "35018", "35019", "35020", "35021",
  "35022", "35023"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 


> 
> 
> Can anyone help? I initially thought strsplit would be the correct
> function for this job, but it's not working properly.
> 
> Thanks,
> Abraham
> 
> 
> I'm using R 2.13 on Windows 7
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list