Pick three of your favorite books on one of your favorite subjects. At least one of the books should have more than one author. For each book, include the title, authors, and two or three other attributes that you find interesting.
Take the information that you’ve selected about these three books, and separately create three files which store the book’s information in HTML (using an html table), XML, and JSON formats (e.g. “books.html”, “books.xml”, and “books.json”). To help you better understand the different file structures, I’d prefer that you create each of these files “by hand” unless you’re already very comfortable with the file formats.
Write R code, using your packages of choice, to load the information from each of the three sources into separate R data frames. Are the three data frames identical? Your deliverable is the three source files and the R code. If you can, package your assignment solution up into an .Rmd file and publish to rpubs.com. [This will also require finding a way to make your three text files accessible from the web]
All documents (XML, HTML and JSON) were created from scratch in text wrangler and verified using publicly available verification tools.
After documents were created each was imported into an r data frame and reviewed for accuracy.
The following packages were used:
xmldataframe <- xmlToDataFrame("books.xml")
colsplit(xmldataframe$authors, split = ";", names=c('author1','author2'))
## author1 author2
## 1 Steven D. Levitt Stephen J. Dubner
## 2 Eric Schlosser
## 3 Martin du Sautoy
dfauthor <- separate(data = xmldataframe, authors, into = c('author1', 'author2'), sep = ";")
## Warning: Too many values at 1 locations: 1
books_xml <- separate(data = dfauthor, attributes, into = c('attribute1', 'attribute2', 'attribute3'), sep = ";")
## Warning: Too many values at 2 locations: 1, 2
colnames(books_xml) <- c("id", "Title", "Release Date", "Author1", "Author2", "Type", "Attribute1", "Attribute2", "Attribute3")
books_xml
## id Title Release Date
## 1 1 Freakonomics 25-AUG-2009
## 2 2 Fast Food Nation: The Dark Side of the All-American Meal 13-MAR-2012
## 3 3 What We Cannot Known 19-May-2016
## Author1 Author2 Type Attribute1
## 1 Steven D. Levitt Stephen J. Dubner Nonfiction Economics
## 2 Eric Schlosser Nonfiction Investigative
## 3 Martin du Sautoy Nonfiction Science
## Attribute2 Attribute3
## 1 Pop culture Social science
## 2 Comment on modern society Food science
## 3 Thought provoking
rawhtml <- readHTMLTable("books.html", header = TRUE, stringsAsFactors = TRUE)
books_html <- data.frame(rawhtml)
colnames(books_html) <- c("id", "Title", "Release Date", "Author1", "Author2", "Type", "Attribute1", "Attribute2", "Attribute3")
books_html
## id Title Release Date
## 1 1 Freakonomics 25-AUG-2009
## 2 2 Fast Food Nation: The Dark Side of the All-American Meal 13-MAR-2012
## 3 3 What We Cannot Known 19-May-2016
## Author1 Author2 Type Attribute1
## 1 Steven D. Levitt Stephen J. Dubner Nonfiction Economics
## 2 Eric Schlosser Nonfiction Investigative
## 3 Martin du Sautoy Nonfiction Science
## Attribute2 Attribute3
## 1 Pop culture Social science
## 2 Comment on modern society Food science
## 3 Thought provoking
rawjson <- jsonlite::fromJSON("books.json", simplifyDataFrame = TRUE)
books_json <- data.frame(rawjson)
colnames(books_json) <- c("id", "Title", "Release Date", "Author1", "Author2", "Type", "Attribute1", "Attribute2", "Attribute3")
books_json
## id Title Release Date
## 1 1 Freakonomics 25-AUG-2009
## 2 2 Fast Food Nation: The Dark Side of the All-American Meal 13-MAR-2012
## 3 3 What We Cannot Known 19-May-2016
## Author1 Author2 Type Attribute1
## 1 Steven D. Levitt Stephen J. Dubner Nonfiction Economics
## 2 Eric Schlosser Nonfiction Investigative
## 3 Martin du Sautoy Nonfiction Science
## Attribute2 Attribute3
## 1 Pop culture Social science
## 2 Comment on modern society Food science
## 3 Thought provoking
Although each file required a different procedure to import the data frames all look the same after formatting the column names.
I found HTML an JSON easiest to work with in terms of creation and importing. This is inspite of the fact I have much more experience using XML in my job.