From f182b6acabf0bc8f8eb15e8c44f0056402f2970b Mon Sep 17 00:00:00 2001 From: aibarra21 Date: Sun, 9 Jun 2024 16:36:41 -0700 Subject: [PATCH] vectorizing the forloop --- R/get_daily_horoscope.R | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/R/get_daily_horoscope.R b/R/get_daily_horoscope.R index 2749888..f07f2bb 100644 --- a/R/get_daily_horoscope.R +++ b/R/get_daily_horoscope.R @@ -28,38 +28,32 @@ getdailyhoroscope <- function(zodiacsign) { # helper function to get only one horoscope by webscrapping horoscope.com get_onedaily_horoscope <- function(page_number){ + Sys.sleep(1) url <- paste0("https://www.horoscope.com/us/horoscopes/general/horoscope-general-daily-today.aspx?sign=", page_number) sign_link <- rvest::read_html(url) # reading the url sign <- sign_link %>% # getting the sign from the webpage rvest::html_node("h1") %>% - rvest::html_text(trim = TRUE) - sign <- stringr::str_replace_all(sign, "Horoscope", "") #trimming excess words - sign <- tolower(sign) + rvest::html_text(trim = TRUE) %>% + stringr::str_replace_all("Horoscope", "") %>% #trimming excess words + tolower() %>% + stringr::str_trim() + daily_horoscope <- sign_link %>% # reading in the daily horoscope rvest::html_node(".switcher+ p") %>% rvest::html_text(trim = TRUE) - horoscope_info <- data.frame(sign, daily_horoscope[1]) #putting into dataframe + horoscope_info <- data.frame(sign, daily_horoscope) #putting into dataframe names(horoscope_info) <- c("sign","horoscope") return(horoscope_info) # return that sign } -# Building data frame with all 12 zodiac signs and their respective daily horoscopes - -horoscope_data <- data.frame(sign = character(), - horoscope = character(), - stringsAsFactors = FALSE) #creating empty dataframe -for (sign in 1:12){ - horoscope_infor <- get_onedaily_horoscope(sign) #looping thru and getting each of 12 signs +# Making data frame with the 12 zodiac signs and their respective daily horoscopes +sign_numbers <- 1:12 +horoscope_list <- lapply(sign_numbers, get_onedaily_horoscope) #using lapply for vectorization +horoscope_data <- bind_rows(horoscope_list) #combining all the data frames into one - horoscope_infor <- as.data.frame(lapply(horoscope_infor, stringr::str_trim), - stringsAsFactors = FALSE) # trimming whitespace - - horoscope_data <- rbind(horoscope_data, horoscope_infor) #adding sign to dataframe - - Sys.sleep(1) #adding a one second pause to not overload server -} +print(horoscope_data) #viewing final dataframe