Code to import data from a Stack overflow query into R

r

Solution

Maybe `textConnection()` is what you want here:

R> zz <- read.table(textConnection("a  b   c
1 11 foo
2 12 bar
3 13 baz
4 14 bar
5 15 foo"), header=TRUE)
R> zz
  a  b   c
1 1 11 foo
2 2 12 bar
3 3 13 baz
4 4 14 bar
5 5 15 foo
R> 

It allows you to treat the text as a "connection" from which to read. You can also just copy and paste, but access from the clipboard is more dependent on the operating system and hence less portable.

Problem

When I try to answer a question in Stack Overflow about R, a good part of my time is spent trying to rebuild the data given as example (unless the question author has been nice enough to provide them as R code). So my question is, if somebody just asks a question and gives his sample data frame the following way : ``` a b c 1 11 foo 2 12 bar 3 13 baz 4 14 bar 5 15 foo ``` Do you have a tip or a function to import this easily into an R session, without having to type the entire `data.frame()` instruction ? Thanks in advance for any hint ! PS : sorry if the term "query" is not really nice in my question title, but it seems you can't use the word "question" in a question title in Stack overflow :-)

Original source