1

I am new-ish to SO and I am curious how you quickly read in data from the questions people post. When someone posts an example data set that looks like this:

x=rnorm(100,0,1)
y=rnorm(100,0,1)
d=cbind(x,y)

I can quickly reproduce it in R. However, I often see people post example data that looks like:

df
   a b c d e f g h i j k l m n o
1  0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
2  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3  0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
4  0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
5  0 1 0 0 1 1 0 0 0 1 1 0 0 1 0
6  0 1 0 0 1 1 0 0 0 0 0 1 1 0 0
7  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8  0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
9  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
11 0 1 1 1 0 1 0 0 0 1 0 0 0 0 1
12 0 1 0 0 0 1 0 1 0 1 0 0 1 0 0
13 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
14 0 1 0 1 0 1 1 0 0 1 1 1 1 1 0
15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
17 0 1 0 1 1 1 0 0 0 0 0 1 1 1 0
18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
20 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0

and I have a hard time quickly reading this into R to answer their question..

User7598
  • 1,658
  • 1
  • 15
  • 28
  • 4
    `read.table(text="text here", header=T, stringsAsFactors = F, sep="") ` maybe this? – Khashaa Mar 25 '15 at 13:57
  • 1
    or `?psych::read.clipboard` – rawr Mar 25 '15 at 14:00
  • 2
    `read.table("clipboard", header=T)` –  Mar 25 '15 at 14:01
  • 1
    See the answers to http://stackoverflow.com/q/5963269/1412059 – Roland Mar 25 '15 at 14:18
  • Thanks @Roland except I'm referring to how to reproduce a data (in order to answer their question) when a great example hasn't been provided as shown in the second example I provided. – User7598 Mar 25 '15 at 14:21
  • 1
    Well, read the answers with less upvotes. There you'll find the options provided in the comments here. Also, of course, the example data in your question is not fully reproducible. You don't know all column classes, all attributes, ... – Roland Mar 25 '15 at 14:25
  • 1
    Also @Khashaa, just curious why do these responses qualify as comments not answers? I see a lot of high rank users do this but the stack meta threads seem to suggest these are better as answers. When I see a high rank user give an answer in the comments (leaving it unanswered) what do you recommend? – User7598 Mar 31 '15 at 12:13

1 Answers1

1

I don't mean to divulge trade secrets, but you can also consider using soread() from the "overflow" package.

With it, you literally copy the sample dataset (ctrl + c) and type soread() and a data.frame named "mydf" would be created in your workspace.

library(overflow)
## Copy the relevant data, including the header
soread() ## can pass some other arguments, but this is generally enough

Example, with the data you shared:

library(overflow)
head(soread()) ## Just using `head` to minimize output
# data.frame “mydf” created in your workspace
#   a b c d e f g h i j k l m n o
# 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
# 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 3 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
# 4 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
# 5 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0
# 6 0 1 0 0 1 1 0 0 0 0 0 1 1 0 0

## Was the object really created?
ls()
# [1] "mydf"
head(mydf)
#   a b c d e f g h i j k l m n o
# 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
# 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 3 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
# 4 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
# 5 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0
# 6 0 1 0 0 1 1 0 0 0 0 0 1 1 0 0
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485