library(rvest)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Defining the URL
namePage <- paste0("https://www.ncbi.nlm.nih.gov/biosample/?term=", 18160380)
# Storing the data from the webpage
testPage <- read_html(namePage)
# Making the data into a table
tableText <- testPage %>%
html_node("table") %>%
html_table()
names(tableText)<-c('Question','Response')
head(tableText)
## # A tibble: 6 × 2
## Question Response
## <chr> <chr>
## 1 body habitat UBERON:feces
## 2 body product UBERON:feces
## 3 tissue UBERON:feces
## 4 geographic location USA
## 5 diet not provided
## 6 dominant hand I am left handed
downloading survey results of samples 31280770-31280775 into a single list consisting of 6 data frames
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
# Creating URLs for all 6 samples
namePages <- paste0(
"https://www.ncbi.nlm.nih.gov/biosample/?term=",
c(31280770, 31280771, 31280772, 31280773, 31280774, 31280775))
# Making 6 survey results into one list with different dataframes
Lists <- lapply(namePages, function(url) {
testPage <- read_html(url)
tableTexts <- testPage %>%
html_node("table") %>%
html_table()
names(tableTexts) <- c("Question", "Response")
return(tableTexts)})
# Checking Column Names
lapply(Lists, names)
## [[1]]
## [1] "Question" "Response"
##
## [[2]]
## [1] "Question" "Response"
##
## [[3]]
## [1] "Question" "Response"
##
## [[4]]
## [1] "Question" "Response"
##
## [[5]]
## [1] "Question" "Response"
##
## [[6]]
## [1] "Question" "Response"
# Checking the first 2 Questions from the surveys in the list made
head(Lists[[1]]$Question, 2)
## [1] "dominant hand" "environmental medium"
head(Lists[[2]]$Question, 2)
## [1] "dominant hand" "environmental medium"
head(Lists[[3]]$Question, 2)
## [1] "dominant hand" "environmental medium"
head(Lists[[4]]$Question, 2)
## [1] "dominant hand" "environmental medium"
head(Lists[[5]]$Question, 2)
## [1] "dominant hand" "environmental medium"
head(Lists[[6]]$Question, 2)
## [1] "dominant hand" "environmental medium"
# Checking the responses of the question that asked which is the participants dominant hand
Lists[[1]][1, ]
## # A tibble: 1 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am right handed
Lists[[2]][1, ]
## # A tibble: 1 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am right handed
Lists[[3]][1, ]
## # A tibble: 1 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am left handed
Lists[[4]][1, ]
## # A tibble: 1 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am left handed
Lists[[5]][1, ]
## # A tibble: 1 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am right handed
Lists[[6]][1, ]
## # A tibble: 1 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am right handed
# Making a new dataframe with the responses of the dominant hand question.
Data <- rbind(
Lists[[1]][1, ],
Lists[[2]][1, ],
Lists[[3]][1, ],
Lists[[4]][1, ],
Lists[[5]][1, ],
Lists[[6]][1, ])
# Checking dataframe
Data
## # A tibble: 6 × 2
## Question Response
## <chr> <chr>
## 1 dominant hand I am right handed
## 2 dominant hand I am right handed
## 3 dominant hand I am left handed
## 4 dominant hand I am left handed
## 5 dominant hand I am right handed
## 6 dominant hand I am right handed
# Making the pie chart
ggplot(Data, aes(x = "", fill = Response)) +
geom_bar() +
coord_polar("y") +
labs(title = "Dominant Hand")