R - call xts via character - r

Assume we have a list of characters as basis for a function, which has a xts object with the same name as result:
library(zoo)
library(xts)
library(quantmod)
l<-list("AAPL","NKE")
for(i in 1:length(l)){
getSymbols(l[[i]], src = "yahoo")
write.zoo(l[[i]], paste(l[[i]],".csv", sep=''), sep = ",")
}
My code does not work, cause getSymbols creates an xts object (named AAPL / NKE). My problem is, that I cannot call them properly in the write.zoo function. Can you please help me?

Call getSymbols with auto = FALSE to get the data directly.
library(quantmod)
syms <- c("AAPL", "NKE")
for(s in syms) {
dat <- getSymbols(s, auto = FALSE)
write.zoo(dat, paste0(s, ".csv"), sep = ",")
}

Here, we need get to get the value of the object created
for(i in 1:length(l)){
getSymbols(l[[i]], src = "yahoo")
write.zoo(get(l[[i]]), paste(l[[i]],".csv", sep=''), sep = ",")
}
-checking

Related

How to make an own function for importing csv?

I would like to make a function to import CSV-files:
# function to read csv
myfunc <- function(x){
paste0(x,"_RAW") <- read.csv(paste0("C:/Users/User/Documents/R/",x,".csv"))
}
einlesen("myvalue")
This does not work. What make I wrong?
The read.csv() function, which is a variant of read.table, returns a data frame. So, the following version of your function might make more sense:
myfunc <- function(x) {
read.csv(paste0("C:/Users/User/Documents/R/", x, ".csv"))
}
# calling returns a data frame representation of the input CSV file
df <- myfunc("myvalue")
Takes directory path and filename, returns dataframe:
read_input_csv <- function(dirpath, filename){
# Load package:
require("readr")
# Import data:
data.frame(read.csv(paste0(dirpath, "/", filename, ifelse(length(grep(".csv", filename)) < 1, ".csv", ""))),
stringsAsFactors = FALSE)
}
# Works for both:
tmp <- read_input_csv("~/Example_dir", "example_filename")
tmp <- read_input_csv("~/Example_dir", "example_filename.csv")

For Loop in R import multiple CSV from URL

I am importing 500 csv's that have the following similar format:
"https://www.quandl.com/api/v3/datasets/WIKI/stockname/data.csv?column_index=11&transform=rdiff&api_key=keyname"
Where stockname is the ticker symbol of a single stock. I have the list of stock tickers saved in a dataframe called stocklist.
I'd like to use lapply to iterate through my list of stocks. Here's what I have so far:
lst <- lapply(stocklist, function(i){
url <- paste0("https://www.quandl.com/api/v3/datasets/WIKI/",i,"/data.csv?column_index=11&transform=rdiff&api_key=XXXXXXXXXXXXXXX")
spdata <- read.csv(url, stringsAsFactors = FALSE)
})
I get the following error:
Error in file(file, "rt") : invalid 'description' argument
What could be causing this error? I tried using a for loop as well, but was unsuccessful and I have been told lapply is a better method in R for this type of task.
Edit:
Stucture of stocklist:
> dput(droplevels(head(stocklist)))
structure(list(`Ticker symbol` = c("MMM", "ABT", "ABBV", "ABMD",
"ACN", "ATVI")), .Names = "Ticker symbol", row.names = c(NA,
6L), class = "data.frame")
Second Edit (solution):
stockdata<-lapply(paste0("https://www.quandl.com/api/v3/datasets/WIKI/",stocklist[1][[1]],"/data.csv?column_index=11&transform=rdiff&api_key=XXXXXXX"),read.csv,stringsAsFactors=FALSE)
Add names to stockdata:
names(stockdata)<-stocklist[1][[1]]
I believe your 'i' variable is a vector.
Make sure you are sub-scripting it properly and only passing one stock at a time.
This: i would look something like this: i[x]
I can't tell what i is, but I'm guessing it's a number. Can you try this and see if it works for you? I think it's pretty close. Just make sure the URL matches the pattern of the actual URL you are fetching data from. I tried to find it; I couldn't find it.
seq <- c(1:10)
for (value in seq) {
mydownload <- function (start_date, end_date) {
start_date <- as.Date(start_date) ## convert to Date object
end_date <- as.Date(end_date) ## convert to Date object
dates <- as.Date("1970/01/01") + (start_date : end_date) ## date sequence
## a loop to download data
for (i in 1:length(dates)) {
string_date <- as.character(dates[i])
myfile <- paste0("C:/Users/Excel/Desktop/", string_date, ".csv")
string_date <- gsub("-", "-", string_date) ## replace "-" with "/"
myurl <- paste("https://www.quandl.com/api/v3/datasets/WIKI/", i, "/data.csv?column_index=11&transform=rdiff&api_key=xxxxxxxxxx", sep = "")
download.file(url = myurl, destfile = myfile, quiet = TRUE)
}
}
}

Search-and-replace on a list of strings - gsub eapply?

Here is a simplified excerpt of my code for reproduction purposes:
library("quantmod")
stockData <- new.env()
stocksLst <- c("AAB.TO", "BBD-B.TO", "BB.TO", "ZZZ.TO")
nrstocks = length(stocksLst)
startDate = as.Date("2016-09-01")
for (i in 1:nrstocks) {
getSymbols(stocksLst[i], env = stockData, src = "yahoo", from = startDate)
}
My data is then stored in this environment stockData which I use to do some analysis. I'd like to clean up the names of the xts objects, which are currently:
ls(stockData)
[1] "AAB.TO" "BB.TO" "BBD-B.TO" "ZZZ.TO"
I want to remove the - and the .TO from all of the names, and have tried to use gsub and eapply, without any success- can't figure out the appropriate syntax. Any help would be appreciated. Thanks.
Using as.list and gsub:
library("quantmod")
stockData <- new.env()
stocksLst <- c("AAB.TO", "BBD-B.TO", "BB.TO", "ZZZ.TO")
nrstocks = length(stocksLst)
startDate = as.Date("2016-09-01")
for (i in 1:nrstocks) {
getSymbols(stocksLst[i], env = stockData, src = "yahoo", from = startDate)
}
ls(stockData)
# [1] "AAB.TO" "BB.TO" "BBD-B.TO" "ZZZ.TO"
#convert to list for ease in manipulation
stockData = as.list(stockData)
#find . and replace everything after it with ""
names(stockData)= gsub("[.].*$","",names(stockData))
#alternately you could match pattern .TO exactly and replace with ""
#names(stockData)= gsub("[.]TO$","",names(stockData))
ls(stockData)
# [1] "AAB" "BB" "BBD-B" "ZZZ"
#convert back to env
list2env(stockData)
Instead of using base R functions like gsub with ?regex while learning R, you may find it much easier to operate on strings with the functions in library stringr. You can use str_replace:
library(stringr)
e.stocks <- list2env(setNames(lapply(stocksLst, function(x) y <- getSymbols(x, env = NULL)),
str_replace(str_replace(stocksLst, "-", ""), "\\.TO", "")))

Looping Issue -Store the data which is of a different format

I am having some trouble storing the data after it runs. The code is picking the files up correctly and running the forecast model but it somehow stores the value for the last file. All the others are lost. Is there anyway that I can have all the results stored in a different array. The problem is that the format of the output is in "forecast" format and because of that I am getting stuck on it. I have looked through all the websites but couldn't find something like that.
Here is the code:
library(forecast)
library(quantmod)
library(forecast)
fileList <-as.array(length(50))
Forecast1 <- as.array(length(50))
fileList<-list.files(path ='C:\\Users\\User\\Downloads\\wOOLWORTHS\\',recursive =T, pattern = ".csv")
i<- integer()
j<-integer()
i=1
setwd("C:\\Users\\User\\Downloads\\wOOLWORTHS\\")
while (i<51)
{
a<-fileList[i]
print(a)
a <- read.csv(a)
fileSales<-a$sales
fileTransform<-log(fileSales)
plot.ts(fileTransform)
result1<-HoltWinters(fileTransform,beta = FALSE,gamma =FALSE,seasonal ="multiplicative",optim.control =TRUE)
result2<-forecast.HoltWinters(result1,h=1)
summary(result1)
accuracy(result2)
#Forecast1[i] <- result2(forecast)
#print(Forecast1[i])
i=i+1
}
It may just be how you are storing your results. Try filling an empty list instead (e.g.Forecast1):
setwd("C:\\Users\\User\\Downloads\\wOOLWORTHS\\")
library(forecast)
library(quantmod)
library(forecast)
fileList <- list.files(path ='C:\\Users\\User\\Downloads\\wOOLWORTHS\\',recursive =T, pattern = ".csv")
Forecast1 <- vector(mode="list", 50)
for(i in seq(length(fileList)){
a <- fileList[[i]]
#print(a)
a <- read.csv(a)
fileSales<-a$sales
fileTransform<-log(fileSales)
plot.ts(fileTransform)
result1<-HoltWinters(fileTransform,beta = FALSE,gamma =FALSE,seasonal ="multiplicative",optim.control =TRUE)
result2<-forecast.HoltWinters(result1,h=1)
#summary(result1)
#accuracy(result2)
Forecast1[[i]] <- result2
#print(Forecast1[i])
print(paste(i, "of", length(fileList), "completed"))
}

Possible bug: setting colnames of an xts object in the last line of a function

I have a R function that follows this form:
CreateXtsFromFile <- function(file)
{
data <- read.table(file, sep = ",")
data <- xts(data[, 1], order.by = data[, 1], unique = FALSE)
# Manipulate the data
colnames(data) <- c("a", "b", "c")
}
However, calling this function returns a character[3]:
data <- CreateXtsFromFile("file.txt")
str(test)
chr [1:2] "a" "b"
However, after removing the final line in the function, the same call returns a xts. The strange thing is that moving the final line to any other line in the function also returns a xts. Has anyone come across this before? I did a search on https://bugs.r-project.org/bugzilla3/ but didn't find anything.
This is not a bug. help("function") tells you what is happening. It says, "If the end of a function is reached without calling return, the value of the last evaluated expression is returned."
So you need to return the data object:
CreateXtsFromFile <- function(file)
{
data <- read.table(file, sep = ",")
data <- xts(data[, 1], order.by = data[, 1], unique = FALSE)
# Manipulate the data
colnames(data) <- c("a", "b", "c")
return(data)
}
Also, you could use as.xts(read.zoo(...)) instead of writing a completely new function from scratch.

Resources