我试图转换一些我报废的财务数据,这已经变得非常令人头痛!我尝试过大多数事情就像使用as.numeric(as.character())
一样,它似乎没有用。真正让我失望的是,如果它是一个角色,它可以使用Close >= 20
进行过滤!我可以绘制它,但不做任何数学运算!不确定这是否是在数据帧中使用它的工件?任何帮助将不胜感激。
# Links to oil futures prices and downloads them
library(dplyr)
oilurl <- "http://quotes.ino.com/exchanges/contracts.html?r=NYMEX_CL"
download.file(oilurl, destfile = ".../Strip/OilStrip.html")
oilhtml <- read_html("..../Strip/OilStrip.html")
# Puts the Oil data into a df
wti_df <- as.data.frame(read_html("..../Strip/OilStrip.html") %>% html_table(fill = TRUE))
colnames(wti_df) <- c("A","Date","C","D","E","Close","G","H","I")
# Cleans up the data into just Dates and Close price, removes any less than $20
wti_df <- select(wti_df,Date,Close)
wti_df <- slice(wti_df, 3:1023)
wti_df <- filter(wti_df,Close >= 20)
#turn "Close" into numeric
transform(wti_df, Close = as.numeric)
我已经尝试了很多东西来使列成为数字,这是我得到的最接近的。这是摘要
> summary(wti_df$Close)
Length Class Mode
[1,] 1 -none- numeric
[2,] 1 -none- numeric
但是,当我尝试做任何事情时,例如在关闭列中添加数字或绘制数据......
> plot(wti_df$Close)
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'
> 5.0 + wti_df$Close
Error in 5 + wti_df$Close : non-numeric argument to binary operator
答案 0 :(得分:1)
&#34;变换&#34;同时给它多个参数时有一些问题(例如要求它首先应用&#34; as.character&#34;然后&#34; as.numeric&#34;到你的专栏。也许尝试一种更简单的方法来看看是否转换是您的问题,或者您的数据是否正在发生其他事情。
wti_df$Close <- as.numeric(as.character(wti_df$Close))