Reputation: 2426
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.
Upvotes: 0
Views: 1479
Reputation: 44525
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.
Upvotes: 2
Reputation: 7396
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?
Upvotes: 2
Reputation: 193517
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.
Upvotes: 1