4

What is the best way to copy and paste spreadsheet data on Stack Overflow?

I've looked on the forums, but I can't seem to find a way to copy and paste headers and their column data. The indenting always seems to come out wrong.

The advanced help does not mention putting data in this field.

5
  • 2
    why would you need to post large data sheet in a post? You should always provide a minimal example. Keep it to the bare minimum.
    – CRABOLO
    Commented Oct 28, 2014 at 2:27
  • 1
    Pasting data is helpful. I don't see where tom says that he needs to post large sets. Fixed-column-width works best. That's the default for R. I don't know if it's supported by Excel, but probably so since Excel can read fixed-column-width data. Then you just indent with Ctrl-K. Commented Oct 28, 2014 at 2:52
  • 1
    Note that unlike certain other markdown implementations, SO's markdown does not have built-in tables. You have to use code-formatting and align stuff yourself.
    – Pokechu22
    Commented Oct 28, 2014 at 3:55
  • Is there any markdown to create tables? on MSE may also be helpful.
    – Ben
    Commented Oct 28, 2014 at 8:19
  • Related: The latest on creating tables using markdown for SO post
    – Wicket
    Commented Oct 26, 2018 at 2:46

1 Answer 1

2

Produce the output with fixed column widths.

You want to paste in a format that can be cut and pasted into another tool. Fixed-width columns are easily readable by both human and machine, so that is a good format to use.

In comparison, comma-separated is easily machine-readable, but not so good for humans.

R prints in fixed-column-width by default to the screen, which makes cut and paste quite convenient. For example:

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Here, > is the prompt character, and I entered the command head(iris) to print that part of this table. Printing this with write.csv to get a CSV equivalent results in this output, which is not human-friendly:

"","Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
"1",5.1,3.5,1.4,0.2,"setosa"
"2",4.9,3,1.4,0.2,"setosa"
"3",4.7,3.2,1.3,0.2,"setosa"
"4",4.6,3.1,1.5,0.2,"setosa"
"5",5,3.6,1.4,0.2,"setosa"
"6",5.4,3.9,1.7,0.4,"setosa"

Even without all those quote characters, it's difficult to tell what goes where.

Last of all, please do not put pipe characters between columns, as is seen in the output from some commands, unless you're demonstrating such a command (e.g., database access from a sql prompt). That is not more human-readable and makes machine-readability harder. Here's an example:

  | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species
1 |          5.1 |         3.5 |          1.4 |         0.2 |  setosa
2 |          4.9 |         3.0 |          1.4 |         0.2 |  setosa
3 |          4.7 |         3.2 |          1.3 |         0.2 |  setosa
4 |          4.6 |         3.1 |          1.5 |         0.2 |  setosa
5 |          5.0 |         3.6 |          1.4 |         0.2 |  setosa
6 |          5.4 |         3.9 |          1.7 |         0.4 |  setosa

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .