I wonder if anyone can explain to me how I can set the type for each variable in my data framework.
I created a function to call quotes from the yahoo API as JSON, and each of them bound each of them to a data framework called a portfolio. The unfortunate data is very raw, and all this happens as characters.
I ask you if there is a simple way by which r can automatically save values as numeric or characters without setting each variable through a data frame.
Here I insert you my script:
library(jsonlite)
nullToNA <- function(x) {
x[sapply(x, is.null)] <- NA
return(x)
}
getQuote <- function(ticker) {
path <- paste("https://query.yahooapis.com/v1/public/yqlq=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22",ticker,"%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=", sep="")
stock <- fromJSON(path, flatten = TRUE)
q <- stock$query$results$quote
q <- nullToNA(q)
}
tickers <- c("AAPL","GOOG", "AMZN")
portfolio <- c()
for (ticker in tickers){
portfolio <- rbind(portfolio, getQuote(ticker))
}
portfolio < unlist(portfolio)
portfolio <- data.frame(portfolio, stringsAsFactors = FALSE)
3 obs 83 , str() , , . ?