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?
# Load libraries
library(XML)
library(jsonlite)
library(RCurl)
# Acquire the data
html_url <- getURL('https://raw.githubusercontent.com/saayedalam/Data/master/books.html')
xml_url <- getURL('https://raw.githubusercontent.com/saayedalam/Data/master/books.xml')
json_url <- getURL('https://raw.githubusercontent.com/saayedalam/Data/master/books.json')
# Parse the data
book_html <- htmlParse(file = html_url)
book_xml <- xmlParse(xml_url)
book_json <- fromJSON(json_url)
# Convert files into a data frame
book_html <- as.data.frame(readHTMLTable(book_html))
root <- xmlRoot(book_xml)
book_xml <- xmlToDataFrame(root)
book_json <- as.data.frame(book_json)
# Manipulate data to have identical dataframes
book_json$my_books.book..id <- NULL # delete index column
colnames(book_html) <- c("Title", "Authors", "Pages", "ISBN", "Goodread Rating") # same column names for all data.fram
colnames(book_xml) <- c("Title", "Authors", "Pages", "ISBN", "Goodread Rating")
colnames(book_json) <- c("Title", "Authors", "Pages", "ISBN", "Goodread Rating")
book_json[] <- lapply(book_json, factor) # change json data to factor
# View the dataframes
book_html
## Title Authors Pages
## 1 Why Buddhism is True Robert Wright 336
## 2 Sapiens Yuval Noah Harari 443
## 3 Automated Data Collection with R Simon Munzert, Christian Rubba 480
## ISBN Goodread Rating
## 1 9781508235408 4.04
## 2 9780525512172 4.45
## 3 9781118834817 4.11
book_xml
## Title Authors Pages
## 1 Why Buddhism is True Robert Wright 336
## 2 Sapiens Yuval Noah Harari 443
## 3 Automated Data Collection with R Simon Munzert, Christian Rubba 480
## ISBN Goodread Rating
## 1 9781508235408 4.04
## 2 9780525512172 4.45
## 3 9781118834817 4.11
book_json
## Title Authors Pages
## 1 Why Buddhism is True Robert Wright 336
## 2 Sapiens Yuval Noah Harari 443
## 3 Automated Data Collection with R Simon Munzert, Christian Rubba 480
## ISBN Goodread Rating
## 1 9781508235408 4.04
## 2 9780525512172 4.45
## 3 9781118834817 4.11
# Check if the dataframes are identical using builtin all.equal function
all.equal(book_html, book_xml)
## [1] TRUE
all.equal(book_html, book_json)
## [1] TRUE
all.equal(book_xml, book_json)
## [1] TRUE