library(rvest)
library(jsonlite)Assignment - Working with HTML and Json
INSTALLING LIBRARIES
READING HTML / JSON FILES
html_page <- read_html("Books.html")
html_table <- html_element(html_page, "table")
## TURN THE TABLES INTO R DATAFRAME
books_from_html <- html_table(html_table)
books_from_html# A tibble: 3 × 5
Title Authors Year Publisher ISBN
<chr> <chr> <int> <chr> <dbl>
1 The Book on Rental Property Investing Brandon T… 2015 BiggerPo… 9.78e12
2 The Millionaire Real Estate Investor Gary Kell… 2005 McGraw H… 9.78e12
3 The Complete Idiot’s Guide to Buying a Home Peter Ric… 2010 DK 9.78e12
## READING THE JSON FILE
books_from_json <- fromJSON("Books.json")
books_from_json title
1 The Book on Rental Property Investing
2 The Millionaire Real Estate Investor
3 The Complete Idiot’s Guide to Buying a Home
authors year publisher
1 Brandon Turner 2015 BiggerPockets Publishing
2 Gary Keller, Dave Jenks, Jay Papasan 2005 McGraw Hill
3 Peter Richmond 2010 DK
isbn
1 9780990711797
2 9780071469531
3 9781101159804
## MAKING BOTH RESULTS AS DATAFRAME
books_from_html <- as.data.frame(books_from_html)
books_from_json <- as.data.frame(books_from_json)
## MAKING EVERY COLUMN AS TEXT IN BOTH DATAFRAMES
books_from_html[] <- lapply(books_from_html, as.character)
books_from_json[] <- lapply(books_from_json, as.character)
print(books_from_html) Title
1 The Book on Rental Property Investing
2 The Millionaire Real Estate Investor
3 The Complete Idiot’s Guide to Buying a Home
Authors Year Publisher
1 Brandon Turner 2015 BiggerPockets Publishing
2 Gary Keller, Dave Jenks, Jay Papasan 2005 McGraw Hill
3 Peter Richmond 2010 DK
ISBN
1 9780990711797
2 9780071469531
3 9781101159804
print(books_from_json) title
1 The Book on Rental Property Investing
2 The Millionaire Real Estate Investor
3 The Complete Idiot’s Guide to Buying a Home
authors year publisher
1 Brandon Turner 2015 BiggerPockets Publishing
2 Gary Keller, Dave Jenks, Jay Papasan 2005 McGraw Hill
3 Peter Richmond 2010 DK
isbn
1 9780990711797
2 9780071469531
3 9781101159804
## COMPARING THE TWO DATAFRAMES
identical(books_from_html, books_from_json)[1] FALSE
names(books_from_html)[1] "Title" "Authors" "Year" "Publisher" "ISBN"
names(books_from_json)[1] "title" "authors" "year" "publisher" "isbn"
## after comparing i observed that the html file has the column headers in uppercase while the json file has it in lowercase and since i prefer uppercase i am going to convert the json to uppercase MAKING BOTH DATAFRAMES UPPERCASE
names(books_from_html) <- toupper(names(books_from_html))
names(books_from_json) <- toupper(names(books_from_json)) COMPARING THE DATAFAMES - CONFIRMATION
identical(books_from_html, books_from_json)[1] TRUE
names(books_from_html)[1] "TITLE" "AUTHORS" "YEAR" "PUBLISHER" "ISBN"
names(books_from_json)[1] "TITLE" "AUTHORS" "YEAR" "PUBLISHER" "ISBN"
CONCLUSION AND FINDINGS
In this assignment, I created HTML and JSON files containing information about three real estate books. I used R to read the information from each file into a data frame. At first, the column headings were formatted differently: the HTML headings began with capital letters, while the JSON headings were lowercase. I changed the headings to a consistent format so R could compare the data correctly. I also made the book titles uppercase in both data frames.
After these changes, I compared the two data frames to check whether they contained the same information. This assignment helped me understand how HTML tables and JSON files organize data, and how R can read and compare information stored in different formats.