Reputation: 1
I'm trying to convert some financial data I scrapped, and it has turned into a big headache! I've tried most things out there like using as.numeric(as.character())
and it does not seem to work. What REALLY throws me off is how it can filter using Close >= 20
if it's a Character!? And I can plot it, but not do any mathematical operations! Not sure if this is an artifact of having it in a dataframe? Any help would be appreciated.
# 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)
I've tried pretty much everything to make the column into a numeric and this is the closest I get. Here is the summary
> summary(wti_df$Close)
Length Class Mode
[1,] 1 -none- numeric
[2,] 1 -none- numeric
But when I try and do anything like add a number to Close column or plot the data....
> 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
Upvotes: 0
Views: 830
Reputation: 136
"Transform" has some issues when giving it multiple arguments simultaneously (such as asking it to first apply "as.character" then "as.numeric" to your column. Maybe try a simpler approach to see if transform is your problem, or if something else is going on with your data.
wti_df$Close <- as.numeric(as.character(wti_df$Close))
Upvotes: 1