-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathgrab_anscombe.R
29 lines (24 loc) · 882 Bytes
/
grab_anscombe.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# grab anscombe's quartet (from wikipedia page) and create JSON file
# read web page
url <- "https://en.wikipedia.org/wiki/Anscombe's_quartet"
library(curl)
con <- curl(url)
the_text <- readLines(con)
close(con)
# grab table
the_table <- XML::readHTMLTable(the_text)[[2]][-1,]
# turn into list of columns, as numbers
the_table <- lapply(the_table, function(a) as.numeric(as.character(a)))
n <- length(the_table[[1]])
# convert to list of data sets
anscombe <- vector("list", 4)
names(anscombe) <- list("I", "II", "III", "IV")
for(i in 1:4) {
anscombe[[i]] <- vector("list", n)
for(j in 1:n) { # unbox to insure x and y are scalars
anscombe[[i]][[j]] <- list(x=jsonlite::unbox(the_table[[i*2-1]][j]),
y=jsonlite::unbox(the_table[[i*2]][j]))
}
}
# write to file
cat(jsonlite::toJSON(anscombe), file="anscombe_quartet.json")