1
我试图将图书存储库中的数据转换成数字数据,以便我可以绘制图表。如何将Web抓取的数据转换为数字?
我的代码目前是:
selector <- ".rrp"
library(rvest)
url <- "https://www.bookdepository.com/bestsellers"
doc <- read_html(url)
prices <- html_nodes(doc, selector)
html_text(prices)
library(readr)
Spiral <- read_csv("C:/Users/Ellis/Desktop/INFO204/Spiral.csv")
View(Spiral)
我试图清理数据:
text <- gsub('[$NZ]', '', Spiral) # removes NZ$ from data
但现在的数据是这样的:
[1] "c(\"16.53\", \"55.15\", \"36.39\", \"10.80\", \"27.57\", \"34.94\",
\"27.57\", \"22.06\", \"22.00\", \"16.20\", \"22.06\", \"22.06\",
\"19.84\", \"19.81\", \"27.63\", \"22.06\", \"10.80\", \"27.57\",
\"22.06\", \"22.94\", \"16.53\", \"25.36\", \"27.57\", \"11.01\",
\"14.40\", \"15.39\")"
,当我尝试运行:
as.numeric(text)
我得到:
Warning message: NAs introduced by coercion
如何清理以这样的方式NZ$
从房价中剔除,我能够绘制的数据了“清理数据”
也许您的数据为因子格式,而不是字符格式。在这种情况下请参阅:[*如何将因子转换为整数\数字而不会丢失信息?](https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-一个整数数字 - 无需-A-失信息) – Jaap