5

Often a SO question includes a table like so:

"v1"    "v2"    "v3"
"A"     "a"      1
"B"     "b"      2
"C"     "c"      3

# or like so

 v1 v2 v3
 A  a  1
 B  b  2
 C  c  3

Rather than a dataframe like so:

df <-  data.frame(v1 = c("A", "B", "C"),
                  v2 = c("a", "b", "c"),
                  v3 = 1:3)

Is there a way to copy and paste the table version of the data from the question and use to work on in one's own console without manually converting the table into a dataframe?

Peter
  • 11,500
  • 5
  • 21
  • 31
  • I often edit layouts like your first table into a tibble using tribble. Easy to do by just adding commas and wrapping everything with tribble(). I agree your 2nd layout would be preferable and require no editing on my part at all. – John Garland Apr 20 '20 at 23:57
  • @ John Yes I do that too but I was hoping there may be a way to avoid adding the commas; its not too bad when there are only 12 to add in the toy dataframe in the question, but I'd still prefer not too add the commas. – Peter Apr 21 '20 at 00:01
  • 2
    an option is `data.table::fread("v1 v2 v3\n1 2 3")` – chinsoon12 Apr 21 '20 at 00:04
  • you may also be interested in `datapasta` https://github.com/MilesMcBain/datapasta which can be added as keyboard shortcuts – user63230 Jul 08 '20 at 13:54

1 Answers1

6

We can use soread from overflow after copying the lines

library(overflow)
soread()
#data.frame “mydf” created in your workspace
#  v1 v2 v3
#1  A  a  1
#2  B  b  2
#3  C  c  3

str(mydf)
#'data.frame':  3 obs. of  3 variables:
# $ v1: chr  "A" "B" "C"
# $ v2: chr  "a" "b" "c"
# $ v3: int  1 2 3

The package can be installed from github

source("http://jtilly.io/install_github/install_github.R")
install_github("mrdwab/overflow-mrdwab")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @akrun I just tried to try out your example with package `overflow` Unfortunately got this message: `Warning in install.packages : package ‘overflow’ is not available (for R version 3.5.3)` – Peter Apr 20 '20 at 23:56
  • 1
    @Peter you can get it from github https://github.com/mrdwab/overflow-mrdwab – akrun Apr 21 '20 at 00:03
  • 1
    would you mind including the source and install_github functions for this package for completeness so that readers do not have to study the small print to find where to access and download the package: `source("http://jtilly.io/install_github/install_github.R") install_github("mrdwab/overflow-mrdwab")` – Peter Apr 21 '20 at 00:15
  • 1
    thanks; This is a package which should be better known. – Peter Apr 21 '20 at 00:21
  • 1
    @Peter I guess people who use SO answering questions know this. It sure gives that 10-20 sec advantage :=) – akrun Apr 21 '20 at 00:22