2

I need to call the same function 10 times, each time with a different year for the end date argument. For example, the end date needs to start as "2011-01-01", then "2012-01-01", then "2013-01-01", etc. How do I loop this function but change the one argument until the last end date is "2020-01-01"?

VI <- mt_subset(product = "MOD13Q1",
                band = "250m_16_days_NDVI",
                lon = 28.5826,
                lat = 1.4368,
                start = "2010-01-01",
                end = "2011-01-01",
                km_lr = 20,
                km_ab = 20,
                site_name = "Ituri test",
                internal = TRUE,
                progress = TRUE)
4
  • 1
    Take a look at do.call. This is what it lives for. I would warn though, hitting the MODIS server up in sequential calls is a sure way to get timed out. I would highly recommend capturing errors, eg., failed download. In this way you can figure out where it failed in downloading data without slogging through the in disk results. You can do this with tryCatch in your looping or call structure. In a for loop you could see a failed download and then move to the next one, which works. Commented Jul 10, 2020 at 22:25
  • @JeffreyEvans do.call? this is more like a plain for or lapply loop isn't it?
    – Spacedman
    Commented Jul 11, 2020 at 6:33
  • @Spacedman, yes but you can split out the parameters into a list the use do.call with lapply and really abbrevte the code. I guess that does negate the advice of using tryCatch Commented Jul 11, 2020 at 12:18
  • @JeffreyEvans no I don't see it. do.call(what, args) is what(args[1], args[2], args[3],...etc...). How is that going to call mt_subset several times? Or are you thinking of do.call(stack, lapply(dates, function(date){mt_subset(..date..)}))?
    – Spacedman
    Commented Jul 11, 2020 at 14:20

1 Answer 1

0

You just have to create a vector of years to iterate on and then assign the value of mt_subset to different objects

# first create the vector of the years
years = c()
for(i in 2011:2020) { 
years = c(years, sprintf("%s-01-01", i))
  }
print(years) # check the vector


# iterate
for(i in 1:(length(years) - 1)) { 
  nam = paste("VI", i, sep = "_") # create names for the objects
  assign(nam) <- mt_subset(product = "MOD13Q1",
                  band = "250m_16_days_NDVI",
                  lon = 28.5826,
                  lat = 1.4368,
                  start = years[i],
                  end = years[i + 1],
                  km_lr = 20,
                  km_ab = 20,
                  site_name = "Ituri test",
                  internal = TRUE,
                  progress = TRUE)
 
}
2
  • 3
    Using assign can be a dangerous thing in the global environment and should not be recommended on code solutions. It would be safer to assign results to something like a list object. Commented Jul 10, 2020 at 22:21
  • Thank you! This was really helpful. I just took out assign, and it served my purposes well. Thank you again. @JeffreyEvans Commented Jul 11, 2020 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.