I am trying to render a table on shinyapps.io, but it is populating with all NA's. I am scraping NCAA basketball spreads from
CodePudding user response:
It seems that the spread_table
after scraping may be post-processed in a way that couldn't convert the extracted substring into numeric
class - i.e. when we do as.numeric
, if there is any character, it may convert to NA
.
In the below code, select
the columns of interest after scraping, then extract
the substring from the 'game_info' column to split into
'date', 'time', 'away_team_name' and 'home_team_name' based on a regex pattern matching and capturing ((...)
) those groups that meet the criteria. (^(\\S )
) - captures the first group as one or more non white spaces characters from the start (^
) of the string, followed by one or more white space (\\s
), then capture characters that are not newline character (([^\n] )
) followed by any character that is not letter ([^A-Za-z]
), capture third groups as one or more characters not the newline followed by again the characters not a letter and capture the rest of the characters ((.*)
). Then loop across
the 'BetMGM' to 'FanDuel', extract the substring characters not having u
or -
and is followed by a space ((?=\\s)
), replace the substring fraction with 0.5
(as there was only a single fraction), loop over the string and eval
utate the string
library(dplyr)
library(tidyr)
library(purrr)
spread_table1 <- spread_table %>%
dplyr::select(game_info, BetMGM, Caesers, FanDuel) %>%
tidyr::extract(game_info, into = c("date", "time", "away_team_name",
"home_team_name"), "^(\\S )\\s ([^\n] )[^A-Za-z] ([^\n] )[^A-Za-z] (.*)") %>%
dplyr::mutate(across(BetMGM:FanDuel, ~
purrr::map_dbl(stringr::str_replace(str_extract(., "-?[^-u] (?=\\s)"),
"(\\d )½", "(\\1 0.5)"), ~ eval(parse(text = .x)))))