For this assignment, I selected three books about Python and manually created the same dataset in both HTML and JSON formats. The data includes the title, authors, publication year, and ISBN for each book.
The HTML and JSON files will be loaded separately into R and converted into data frames. After loading the data, I will compare the two data frames to determine whether they are identical. This may require checking the structure and data types since the two file formats can read the same information differently.
Load data
The same book information was manually created in two formats HTML and JSON, and I will load each one separately into R.
After loading the HTML and JSON files into separate data frames, I compared them using identical(). I also used str() to check the structure and data types of each data frame in case they were not identical.
Show code
identical(html_books, json_books)
[1] FALSE
Show code
str(html_books)
tibble [3 × 4] (S3: tbl_df/tbl/data.frame)
$ Title : chr [1:3] "Python Crash Course: A Hands-on, Project-based Introduction to Programming" "Introduction to Machine Learning with Python: A Guide for Data Scientists" "Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter"
$ Authors: chr [1:3] "Eric Matthes" "Andreas C. Müller, Sarah Guido" "Wes McKinney"
$ Year : int [1:3] 2023 2016 2022
$ ISBN : num [1:3] 9.78e+12 9.78e+12 9.78e+12
Show code
str(json_books)
'data.frame': 3 obs. of 4 variables:
$ Title : chr "Python Crash Course: A Hands-on, Project-based Introduction to Programming" "Introduction to Machine Learning with Python: A Guide for Data Scientists" "Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter"
$ Authors: chr "Eric Matthes" "Andreas C. Müller, Sarah Guido" "Wes McKinney"
$ Year : int 2023 2016 2022
$ ISBN : chr "9781718502703" "9781449369897" "9781098104030"
The comparison returned FALSE. The values are the same, but the data types are slightly different. The HTML file reads ISBN as numeric, while the JSON file reads ISBN as character. The HTML data is also stored as a tibble, while the JSON data is a regular data frame.
Since ISBN is an identifier rather than a value used for calculations, I converted the ISBN column in the HTML data from numeric to character, and also converted it to regular data frame.
Now we can check if both data frames are identical
Show code
identical(html_books, json_books)
[1] TRUE
After making the data types and structures consistent, the comparison returned TRUE. This confirms that the HTML and JSON files contain the same book information.
Summary
In this assignment, I created the same book dataset in HTML and JSON formats and loaded both files into R. Although the data contained the same information, R initially read the ISBN and data frame structures differently. After making these consistent, the two data frames were identical. This exercise showed how the same data can be stored and read differently depending on the file format.
Source Code
---title: "Assignment 7"author: "Anastasiia Gmyrina"editor: visualformat: html: theme: cosmo toc: true toc-location: left toc-depth: 3 code-fold: true code-summary: "Show code" code-tools: true df-print: paged smooth-scroll: true embed-resources: true---## ApproachFor this assignment, I selected three books about Python and manually created the same dataset in both HTML and JSON formats. The data includes the title, authors, publication year, and ISBN for each book.The HTML and JSON files will be loaded separately into R and converted into data frames. After loading the data, I will compare the two data frames to determine whether they are identical. This may require checking the structure and data types since the two file formats can read the same information differently.## Load dataThe same book information was manually created in two formats HTML and JSON, and I will load each one separately into R.### Loading HTML```{r}#| message: falselibrary(rvest)library(dplyr)html_data <-read_html("https://raw.githubusercontent.com/antristesse/DATA607/refs/heads/main/week%207/books.html") all_tables <- html_data %>%html_table(fill =TRUE)html_books <- all_tables[[1]]html_books```### Loading JSON```{r}library(jsonlite)json_books <-fromJSON("https://raw.githubusercontent.com/antristesse/DATA607/refs/heads/main/week%207/books.json")json_books```## Compare DataAfter loading the HTML and JSON files into separate data frames, I compared them using `identical()`. I also used `str()` to check the structure and data types of each data frame in case they were not identical.```{r}identical(html_books, json_books)str(html_books)str(json_books)```The comparison returned FALSE. The values are the same, but the data types are slightly different. The HTML file reads ISBN as numeric, while the JSON file reads ISBN as character. The HTML data is also stored as a tibble, while the JSON data is a regular data frame.Since ISBN is an identifier rather than a value used for calculations, I converted the ISBN column in the HTML data from numeric to character, and also converted it to regular data frame.```{r}html_books <- html_books %>%mutate(ISBN =as.character(ISBN)) %>%as.data.frame()```Now we can check if both data frames are identical```{r}identical(html_books, json_books)```After making the data types and structures consistent, the comparison returned TRUE. This confirms that the HTML and JSON files contain the same book information.## SummaryIn this assignment, I created the same book dataset in HTML and JSON formats and loaded both files into R. Although the data contained the same information, R initially read the ISBN and data frame structures differently. After making these consistent, the two data frames were identical. This exercise showed how the same data can be stored and read differently depending on the file format.