1

I have two lists, which I want to use to create a new list that is a product of the length of the two lists. I have code that does this with for loops, but I would like to do it in one line with something vectorized like lapply or map2. Thanks!

years <- c(1999, 2000, 2001)
file <- list("tmin", "tmax")
var <- vector()
for(ys in years){
  #determine which days need to be extracted
  for(f in file){
  var <- append(var, paste0(f, ".", ys, ".nc"))
  }
}
Cannon
  • 67
  • 6
  • Looks like this is the exact same question, but I don't think I would have found it with the keywords I was using. Thanks! – Cannon Mar 08 '21 at 18:33

1 Answers1

1

Unless this is a simplified example and something more is needed to scale, this will produce the same output:

paste(file, rep(years, each = length(file)), 'nc', sep = '.')

[1] "tmin.1999.nc" "tmax.1999.nc" "tmin.2000.nc" "tmax.2000.nc" "tmin.2001.nc" "tmax.2001.nc"
manotheshark
  • 4,297
  • 17
  • 30