1. 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”).
    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?
library(XML)
library(RCurl)
library(rjson)
library(rvest)
library(rlist)
library(plyr)
library(jsonlite)
library(knitr)
library(stringr)

1 HTML- books.html

html.url <- "https://raw.githubusercontent.com/keshaws/CUNY_MSDS_2020/master/DATA607/books.html"
html.file <- getURLContent(url = html.url)
books.html <-  html.file %>%
                readHTMLTable()
books.html.df <- as.data.frame(books.html)
colnames(books.html.df) <- str_replace(colnames(books.html.df),"NULL\\.", "")
colnames(books.html.df) <- str_replace(colnames(books.html.df),"\\.", " ")
kable(books.html.df)
Title Authors Type Length AmazonRating
The 4 Disciplines of Execution: Achieving Your Wildly Important Goals Chris McChesney Paperpack 352 5
Change or Die: The Three Keys to Change at Work and in Life Alan Deutschman Paperpack 256 4.5
R for Data Science: Import, Tidy, Transform, Visualize, and Model Data Hadley Wickham; Garrett Grolemund Paperpack 522 4.5
Data Science for Business: What You Need to Know about Data Mining and Data-Analytic Thinking Foster Provost; Karen Dillon; Tom Fawcett Paperpack 414 4.5
Upstream: The Quest to Solve Problems Before They Happen Dan Heath Kindle 320 4.5
dim(books.html.df)
## [1] 5 5

2 XML-books.xml

xml.url <- "https://raw.githubusercontent.com/keshaws/CUNY_MSDS_2020/master/DATA607/books.xml"
xml.file <- getURLContent(xml.url)
xml.df <- xml.file %>%
  xmlParse() %>%
  xmlToDataFrame()
dim(xml.df)
## [1] 5 5
kable(xml.df)
Title Author Type Length AmazonRating
The 4 Disciplines of Execution: Achieving Your Wildly Important Goals Chris McChesney Paperpack 352 5
Change or Die The Three Keys to Change at Work and in Life Alan Deutschman Paperpack 256 4.5
R for Data Science: Import, Tidy, Transform, Visualize, and Model Data Hadley Wickham; Garrett Grolemund Paperpack 522 4.5
Data Science for Business: What You Need to Know about Data Mining and Data-Analytic Thinking Foster Provost; Karen Dillon; Tom Fawcett Paperpack 414 4.5
Upstream: The Quest to Solve Problems Before They Happen Dan Heath Kindle 320 4.5

3 XML-books.json

json.url <- "https://raw.githubusercontent.com/keshaws/CUNY_MSDS_2020/master/DATA607/books.json"
json.file <- getURLContent(json.url)
json.df <- as.data.frame(fromJSON(json.file[[1]]))
colnames(json.df) <- str_replace(colnames(json.df),"books\\.", "")
colnames(json.df) <- str_replace(colnames(json.df),"\\.", " ")
dim(json.df)
## [1] 5 5
kable(json.df)
Title Authors Type Length AmazonRating
The 4 Disciplines of Execution: Achieving Your Wildly Important Goals Chris McChesney Paperpack 288 4.5
Change or Die The Three Keys to Change at Work and in Life Alan Deutschman Paperpack 256 4.5
R for Data Science: Import, Tidy, Transform, Visualize, and Model Data c(“Hadley Wickham”, “Garrett Grolemund”) Paperpack 522 4.5
Data Science for Business: What You Need to Know about Data Mining and Data-Analytic Thinking c(“Foster Provost”, “Karen Dillon”, “Tom Fawcett”) Paperpack 414 4.5
Upstream: The Quest to Solve Problems Before They Happen Dan Heath Paperpack 320 4.5
books.html.df ==json.df
##      Title Authors  Type Length AmazonRating
## [1,]  TRUE    TRUE  TRUE  FALSE        FALSE
## [2,] FALSE    TRUE  TRUE   TRUE         TRUE
## [3,]  TRUE   FALSE  TRUE   TRUE         TRUE
## [4,]  TRUE   FALSE  TRUE   TRUE         TRUE
## [5,]  TRUE    TRUE FALSE   TRUE         TRUE
json.df==xml.df
##   Title Authors  Type Length AmazonRating
## 1  TRUE    TRUE  TRUE  FALSE        FALSE
## 2  TRUE    TRUE  TRUE   TRUE         TRUE
## 3  TRUE   FALSE  TRUE   TRUE         TRUE
## 4  TRUE   FALSE  TRUE   TRUE         TRUE
## 5  TRUE    TRUE FALSE   TRUE         TRUE

We have successfully handled all three data file formats: html, xml and json format. We can see that all three data frames: html, xml, json are not identicial.