0

I have a dataframe where some columns contain levels with spaces. When I call the dataframe and copy paste the result into read.table(text="") it doesn't work because there is an unequal number of spaces in different rows. So how to do a clean display of the dataframe in the first place so I can copy paste it into read.table specifying the seperator so I can do a reproducible example quickly ? Also how to delete the automatic numerotation (1,2,3,...) ?

> tdat
     uL Intensity                      sample
1   6.0  29355.00 PCAM MCH LOW-atp,E1E2,UbK48
2   4.0  36034.00 PCAM MCH LOW-atp,E1E2,UbK48
3   2.0  42571.00 PCAM MCH LOW-atp,E1E2,UbK48
4   1.0  62325.00 PCAM MCH LOW-atp,E1E2,UbK48
5   0.5  79505.00 PCAM MCH LOW-atp,E1E2,UbK48
6  25.0  25190.00                    MCH Mild
7  20.0  19721.50                    MCH Mild

In this dataframe I have 3 columns, I would like R to display a separator between each column so I can use read.table easily.

Krysta
  • 220
  • 3
  • 11
Wicelo
  • 2,358
  • 2
  • 28
  • 44
  • 1
    Could `dput` work for you? – Tyler Rinker Jul 02 '13 at 18:40
  • @Tyler gives a good suggestion. But why do you want to be able to do this exactly? – Thomas Jul 02 '13 at 18:41
  • I want to be able to do a reproducible example quickly by the mean of a simple copy-paste directly from my working data. – Wicelo Jul 02 '13 at 18:58
  • I think then you should look at answers to [this question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). There are also some functions (exact form may depend on your OS) that can write objects to the clipboard. – Thomas Jul 02 '13 at 19:06
  • I've added an answer per (my previous comment) that involves writing directly to the clipboard, which maybe is what you're going for. – Thomas Jul 02 '13 at 19:25

3 Answers3

2

Your chief concern seems to be making a reproducible example, so, in light of that, there are a couple solutions that come to mind.

The first is to use write.table:

> write.table(iris, row.names=F)
"Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
5.1 3.5 1.4 0.2 "setosa"
4.9 3 1.4 0.2 "setosa"
4.7 3.2 1.3 0.2 "setosa"

The second is to use dput:

> dput(iris[1:2, ])
structure(list(Sepal.Length = c(5.1, 4.9), Sepal.Width = c(3.5, 3), Petal.Length = c(1.4, 1.4), Petal.Width = c(0.2, 0.2), Species = structure(c(1L, 1L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), .Names = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), row.names = 1:2, class = "data.frame")

Someone on StackOverflow could copy this output and assign it to a name:

> my.data <- structure(list(Sepal.Length = c(5.1, 4.9), Sepal.Width = c(3.5, 3), Petal.Length = c(1.4, 1.4), Petal.Width = c(0.2, 0.2), Species = structure(c(1L, 1L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), .Names = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species"), row.names = 1:2, class = "data.frame")
> my.data
  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

You should probably take a look at this question: How to make a great R reproducible example?

Community
  • 1
  • 1
Peyton
  • 7,266
  • 2
  • 29
  • 29
2

The best answer to this question, which seems to be about copy/pasting an R object to clipboard is to do:

dput(tdat, file="clipboard")

This uses the clipboard as a file connection, which saves any manual copy/paste.

Thomas
  • 43,637
  • 12
  • 109
  • 140
1

This question doesn't really make much sense as it's currently phrased--or, at least I can't really think of a use-case for what you describe.

Nevertheless, here is a workaround: Use print with quote = TRUE. With your "tdat"

> print(tdat, quote = TRUE)
      uL Intensity                        sample
1 " 6.0" "29355.0" "PCAM MCH LOW-atp,E1E2,UbK48"
2 " 4.0" "36034.0" "PCAM MCH LOW-atp,E1E2,UbK48"
3 " 2.0" "42571.0" "PCAM MCH LOW-atp,E1E2,UbK48"
4 " 1.0" "62325.0" "PCAM MCH LOW-atp,E1E2,UbK48"
5 " 0.5" "79505.0" "PCAM MCH LOW-atp,E1E2,UbK48"
6 "25.0" "25190.0"                    "MCH Mild"
7 "20.0" "19721.5"                    "MCH Mild"

This can then be read in as you describe:

## Note the single quote below
read.table(text = '<<the stuff you copied from print(tdat, quote = TRUE)>>', ...)

All the recommendations to use dput() and so on are most likely the direction you should be looking.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 29 did not have 4 elements – Wicelo Jul 02 '13 at 18:59