0

I am new to R and having a bit of trouble setting up functions. I am trying to run a backtest in a large number of assets or markets. All of this data is in CSV.

I have a dataframe that is basically a list of the markets [asset] and their characteristics [assetlist] that I want to open in my enviroment so that I can use to test. I will need to open them quite frequently since they are daily updated as a CSV file.

My main goal is to get the CSV files that are downloaded daily and convert them to a dataframe in R.

I tried setting up the function below, I do get the print out on my console but the markets [asset] don´t show up in my enviroment.

# this is the function I set up to upload the asset list with the markets and their features/characteristics and in the loop I go through each of their files.
trading.opencsv <- function(rd){
      asset.directory <- paste(rd, "list.csv", sep="")
      assetlist <<- read.csv(asset.directory, stringsAsFactors=FALSE)
      print(assetlist$Market)
      for (i in 1:nrow(assetlist)){
        asset <- assetlist$Market[i]
        x.dir <- paste(rd, asset, ".csv", sep="")
        x <- read.csv(x.dir)
        print(asset)
        assign(asset, x)
      }

    }

#this is the directory I use to save the csv files and running the function.
rd <- "C:/Users/augus/Dropbox/Trading/R/Trading/Dados/"
trading.opencsv(rd)
Augusto
  • 51
  • 1
  • 6
  • 1
    I think I understand what you're asking, but just to clarify -- your goal is to take several CSV files that have the same columns (but just new observations of those variables), and read them into R such that the data from every CSV file is in one dataframe? – duckmayr Nov 18 '17 at 14:02
  • Because the function has its own environment… you habe to specify within `assign`, that you want to save them within a certain environment. In your case you seem to expect it to appear in the global environment, hence give it as argument. But be aware that in general, `assign` is always a bad choice. Use lists instead. – Tino Nov 18 '17 at 14:03
  • duckmayr - Yes several CSV files with the same columns but with different observation. – Augusto Nov 18 '17 at 14:37
  • Tino, thanks, why would assign be a bad choice? also, do you know how I would be able to assign to the global enviroment? – Augusto Nov 18 '17 at 14:38

1 Answers1

0
library(dplyr)
library(readr)
library(purrr)

trading.opencsv <- function(rd) {
  asset.directory <- paste0(rd, "list.csv")
  assetlist <- read_csv(asset.directory)
  print(assetlist$Market)
  map(assetlist$Market, ~ read_csv(paste0(rd, .x, ".csv"))) %>%
    bind_rows()
}
amarchin
  • 2,044
  • 1
  • 16
  • 32