【发布时间】:2017-09-03 10:49:01
【问题描述】:
我正在尝试将从图书存放处抓取的数据转换为数字数据,以便我可以绘制它。
我目前的代码是:
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$ 从价格中删除并且我能够绘制“清理后的数据”
【问题讨论】:
-
也许您的数据是因子格式而不是字符格式。在这种情况下,请参阅:How to convert a factor to an integer\numeric without a loss of information?
标签: r type-conversion rvest