Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Looping over a set of dataframes in R

$
0
0

I an new to R and have a quick doubt (have gone through a lot of questions on stack-overflow but to no avail).

I have created a function (as can be seen in my code) where x and y are dates and $z_{1} to z_{9}$ are data-frames. The function goes through the 9 files subsets the data depending on the given dates and returns a merged data-set.

DATE1_May <- as.Date("2017-11-16")
DATE2_May <- as.Date("2018-02-15")

myfunc1 <- function(x,y,z1,z2,z3,z4,z5,z6,z7,z8,z9){
  a1 <- z1[z1$Date >= x & z1$Date <= y,]
  b1 <- a1[c(1,2)]
  b1 <- data.frame(b1)
  a2 <- z2[z2$Date >= x & z2$Date <= y,]
  b2 <- a2[c(1,2)]
  b2 <- data.frame(b2)
  a3 <- z3[z3$Date >= x & z3$Date <= y,]
  b3 <- a3[c(1,2)]
  b3 <- data.frame(b3)
  a4 <- z4[z4$Date >= x & z4$Date <= y,]
  b4 <- a4[c(1,2)]
  b4 <- data.frame(b4)
  a5 <- z5[z5$Date >= x & z5$Date <= y,]
  b5 <- a5[c(1,2)]
  b5 <- data.frame(b5)
  a6 <- z6[z6$Date >= x & z6$Date <= y,]
  b6 <- a6[c(1,2)]
  b6 <- data.frame(b6)
  a7 <- z7[z7$Date >= x & z7$Date <= y,]
  b7 <- a7[c(1,2)]
  b7 <- data.frame(b7)
  a8 <- z8[z8$Date >= x & z8$Date <= y,]
  b8 <- a8[c(1,2)]
  b8 <- data.frame(b8)
  a9 <- z9[z9$Date >= x & z9$Date <= y,]
  b9 <- a9[c(1,2)]
  b9 <- data.frame(b9)
  fin1 <- Reduce(function(x, y) merge(x, y, all=T, by=c("Date")), list(b1,b2,b3,b4,b5,b6,b7,b8,b9))
  }
Testx1 <- myfunc1(DATE1_May,DATE2_May, May18,July18, September18, December18,March19, May19, July19, September19, December19)    

I have 2 questions:

  1. I have written this code for a March18 futures contract. I want to do a similar thing with March17 contract but in that case $z_{1} to z_{9}$ will be from May17 to December18. And, the dates would be:

    DATE1_May <- as.Date("2016-11-16")
    DATE2_May <- as.Date("2017-02-15")
    

    I was trying to create a for loop and use assign command. However, I am not certain how to do so. Is there a way to automate this? (Right now, I am creating separate functions but it's taking a lot of time since I have to do this for over 100 contracts.)

  2. Is there a way to shorten the function (It works perfectly fine though).


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>