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