- 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)
## Loading required package: bitops
library(rjson)
library(rvest)
## Warning: package 'rvest' was built under R version 3.4.4
## Loading required package: xml2
##
## Attaching package: 'rvest'
## The following object is masked from 'package:XML':
##
## xml
## Warning: package 'rlist' was built under R version 3.4.4
library(plyr)
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 3.4.4
##
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:rjson':
##
## fromJSON, toJSON
library(knitr)
library(stringr)
html.url <- "https://raw.githubusercontent.com/niteen11/MSDS/master/DATA607/Week7/dataset/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)
| How to Win Friends & Influence People |
Dale Carnegi |
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 |
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 |
| The Four: The Hidden DNA of Amazon, Apple, Facebook, and Google |
Scott Galloway |
Kindle |
320 |
4.5 |
## [1] 5 5
xml.url <- "https://raw.githubusercontent.com/niteen11/MSDS/master/DATA607/Week7/dataset/books.xml"
xml.file <- getURLContent(xml.url)
xml.df <- xml.file %>%
xmlParse() %>%
xmlToDataFrame()
dim(xml.df)
## [1] 5 5
| How to Win Friends & Influence People |
Dale Carnegi |
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 |
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 |
| The Four: The Hidden DNA of Amazon, Apple, Facebook, and Google |
Scott Galloway |
Kindle |
320 |
4.5 |
json.url <- "https://raw.githubusercontent.com/niteen11/MSDS/master/DATA607/Week7/dataset/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
| How to Win Friends &Influence People |
Dale Carnegi |
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 |
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 |
| The Four: The Hidden DNA of Amazon, Apple, Facebook, and Google |
Scott Galloway |
Paperpack |
320 |
4.5 |
## Title Authors Type Length AmazonRating
## [1,] FALSE TRUE TRUE TRUE TRUE
## [2,] FALSE TRUE TRUE TRUE TRUE
## [3,] TRUE FALSE TRUE TRUE TRUE
## [4,] TRUE FALSE TRUE TRUE TRUE
## [5,] TRUE TRUE FALSE TRUE TRUE
## Title Authors Type Length AmazonRating
## 1 FALSE TRUE TRUE TRUE TRUE
## 2 TRUE TRUE TRUE TRUE TRUE
## 3 TRUE FALSE TRUE TRUE TRUE
## 4 TRUE FALSE TRUE TRUE TRUE
## 5 TRUE TRUE FALSE TRUE TRUE
All three data frames: html, xml, json are not identicial