Atomic vector error when using R package `stringr`

806 views Asked by At

I want to use the packages rvest to pull gas prices from a web page. However, I can not pull the numeric values and have to pull by the html class .sp_p.

library(rvest)
desmoines <- html("http://www.desmoinesgasprices.com/")

Pull gas prices:

price <- desmoines %>%
  html_nodes(".sp_p")

head(price, 3)

Output:

[[1]]
<div class="sp_p">
  <div class="p2"></div>
  <div class="pd"></div>
  <div class="p5"></div>
  <div class="p5"></div>
</div> 

[[2]]
<div class="sp_p">
  <div class="p2"></div>
  <div class="pd"></div>
  <div class="p5"></div>
  <div class="p6"></div>
</div> 

[[3]]
<div class="sp_p">
  <div class="p2"></div>
  <div class="pd"></div>
  <div class="p5"></div>
  <div class="p7"></div>
</div> 

attr(,"class")
[1] "XMLNodeSet"

Now, I want to use the package stringr to extract the digits from the web scrape, but I can't use stringr because price is not an atomic vector. How do I get around this?

1

There are 1 answers

5
RHertel On BEST ANSWER

Here's one possibility:

library(stringr)
pr <- xml_children(price)
p_raw <- sapply(1:length(pr), function(x) paste(xml_attrs(pr[[x]]),collapse=""))
p_readable <- paste0("$",str_replace_all(p_raw,c("d"=".","p"="")))

#> p_readable
# [1] "$2.49" "$2.57" "$2.59" "$2.59" "$2.59" "$2.59" "$2.59" "$2.59" "$2.61" "$2.64" "$2.67" "$2.68" "$2.68"
#[14] "$2.68" "$3.08" "$2.99" "$2.98" "$2.98" "$2.98" "$2.98" "$2.98" "$2.98" "$2.98" "$2.98" "$2.98" "$2.98"
#[27] "$2.98" "$2.98" "$2.98"