Skip to content

Instantly share code, notes, and snippets.

@romainx
Last active May 5, 2018 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainx/07a5a5dfeb663f29abebb92b14a64f05 to your computer and use it in GitHub Desktop.
Save romainx/07a5a5dfeb663f29abebb92b14a64f05 to your computer and use it in GitHub Desktop.
One of the tedious thing in Stack Overflow is to grab example data provided by users in order to be able to use it to reproduce the case and try to solve it. Here is how to do it efficiently in R and Python, hope this will help you being the first to answer!
import pandas as pd
import io
# Paste the text by using of triple-quotes to span String literals on multiple lines
zz = """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
3 4.7 3.2 1.3 0.2 setosa
"""
df = pd.read_table(io.StringIO(zz), delim_whitespace=True)
df.head()
## 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
## 3 4.7 3.2 1.3 0.2 setosa
# It is also possible to read directly the text from the clipboard – under the hood this function calls read_table.
df = pd.read_clipboard()
library(tibble)
# Paste the text in a String, R allows multiline strings.
zz <- "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
3 4.7 3.2 1.3 0.2 setosa
"
# I have not found the way to do the same thing with one of the readr function :-(
df <- read.table(text = zz, header = TRUE) %>% as_data_frame()
df %>% head()
## # A tibble: 3 × 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fctr>
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
# And this is how to read data directly from the clipboard thanks to the psych package.
library(psych)
df <- read.clipboard()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment