0

I would like to create a .csv of a data frame called "Extended". When viewing a file I had to work with "carwaytempshort.csv" it has a nice layout, the columns and rows are neatly lined up. When viewing the data.frame "Extended" it looks much like "carwaytempshort.csv" with the neat alignment of rows and columns. However, when using write.csv or write.table, "Extended.csv" is very unattractive.The columns and rows aren't lined up, instead each datum has quotation marks and some rows are two lines.

I've tried the following lines of code:

> write.table(Extended, "Extended.csv")

as well as:

> write.csv(Extended, "Extended.csv")

This is what I would like "Extended.csv" to look like (this is what "Extended" looks like):

enter image description here

This is what I get:

enter image description here

Ben
  • 103
  • 5
  • 3
    The first file is not CSV (despite the extension). It's fixed width columns. CSV files are meant to be machine-processible, not to be human-readable. If you want pretty text, use a different format than CSV. – Ken White Feb 16 '18 at 17:10
  • 1
    take a look with text editor, the file seems tab-seperated – sequoia Feb 16 '18 at 17:12
  • 1
    CSV stands for comma-separated values. So it's supposed to be a bunch of values separated by commas. If you want spaces do they line up and no commas, then you might want to consider a fixed width format. Also, if you have quotes around your numbers, that likely means the data in your data.frame is of the wrong class. And in your first pick you seem to be using the rstudio table viewer -- you are not looking at the raw contents of a file. – MrFlick Feb 16 '18 at 17:12
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions – MrFlick Feb 16 '18 at 17:13
  • If you open Extended.csv in a spreadsheet program (such as excel), it should look like what you want. – Pdubbs Feb 16 '18 at 17:26
  • As it stands, it is not possible to give a precise answer to your question. Where do you want to view the file? In a spreadsheet? In R console? In R file viewer? In a generated HTML or PDF file? As noted by @MrFlick, you should make things reproducible, e.g. include `head(Extended, 2)` to let people know the format of the original data, or `dput(head(Extended, 2))` – antonio Feb 16 '18 at 17:49
  • @Ben did you try to add the sep argument in you write.table() `write.table(Extended, "Extended.csv", sep = ",")` – Michael Vine Feb 16 '18 at 18:12

1 Answers1

0

Using the built-in anscombe data frame, use sprintf to format the lines in any way you like:

fmt <- paste(rep("%10s", ncol(anscombe)), collapse = ",")
outLines <- c(do.call("sprintf", as.list(c(fmt, names(anscombe)))), 
      do.call("sprintf", c(fmt, anscombe)))
writeLines(outLines, stdout())

giving:

x1,        x2,        x3,        x4,        y1,        y2,        y3,        y4
10,        10,        10,         8,      8.04,      9.14,      7.46,      6.58
 8,         8,         8,         8,      6.95,      8.14,      6.77,      5.76
13,        13,        13,         8,      7.58,      8.74,     12.74,      7.71
 9,         9,         9,         8,      8.81,      8.77,      7.11,      8.84
11,        11,        11,         8,      8.33,      9.26,      7.81,      8.47
14,        14,        14,         8,      9.96,       8.1,      8.84,      7.04
 6,         6,         6,         8,      7.24,      6.13,      6.08,      5.25
 4,         4,         4,        19,      4.26,       3.1,      5.39,      12.5
12,        12,        12,         8,     10.84,      9.13,      8.15,      5.56
 7,         7,         7,         8,      4.82,      7.26,      6.42,      7.91
 5,         5,         5,         8,      5.68,      4.74,      5.73,      6.89
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341