3

I am trying to write a loop where I apply a function to a list of lists. I want to interpolate the temperatures (max mean temperature/MAXMEAN) of different countries with the idw command of the gstat package.

The idw function needs the information of the coordinates (longitude, latitude) and the variable MAXMEAN. These are combined in the data frame list "temperatures.coordinates" (looks like this: https://i.stack.imgur.com/ZNkZp.jpg) which is a list of 37 countries, where each list is seperated again into 12 months (sublist length of 12). The function also needs information from the grd.list dataframe which is a list of length 37 without a sublist.

Applied to a single country and single month, the code looks like this:

# Create a gridded structure

grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.1), y = seq(from = y.range[1], to = y.range[2], by = 0.1))
coordinates(grd) <- ~x + y
gridded(grd) <- TRUE

#Interpolate surface and fix the output. Apply idw model for the data

idw <- idw(formula = MAXMEAN ~ 1, locations = temperatures.coordinates, newdata = grd)  

I've tried to do this:

# Create a gridded structure
grd <- list()
for (i in 1:length(countrybounds)) {
  grd[[i]] <- expand.grid(x = seq(from = (x.range[[i]])[1], to = (x.range[[i]])[2], by = 0.1), y = seq(from = (y.range[[i]])[1], to = (y.range[[i]])[2], by = 0.1))
  coordinates(grd[[i]]) <- ~x + y
  gridded(grd[[i]]) <- TRUE
  grd.list <- grd
  gridded(grd.list[[i]]) <- TRUE 
  }

#Interpolate surface and fix the output. Apply idw model for the data

idw.list <- list()
for (i in 1:length(grd.list)) {
idw.list[[i]] <- list()
for (j in 1:length(grd[[i]])) {
idw.list[[i]][[j]] <- idw(formula = MAXMEAN ~ 1, locations = (temperatures.coordinates[[i]][[j]]), newdata = grd.list[[i]])
}
}

I receive this error after running the loop:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘idw’ for signature ‘"formula", "data.frame"’

This has worked in a similar previous loop, where I create the list of lists for the temperature coordinates:

temperatures.coordinates <- list()
for(i in 1:length(monthlymean)){
  temperatures.coordinates[[i]] <- list()
  for(j in 1:length(monthlymean[[i]])){
    temperatures.coordinates[[i]][[j]]<-(monthlymean[[i]][[j]])[,c("LON","LAT","MAXMEAN")]
  }
}

I'm not sure if I gave all the relevant info and I've been stuck on this for a while - appreciate any help, thanks!

Lisa
  • 33
  • 4
  • 4
    It is difficult to find problems in your code without a sample of your data. If you can, the easiest way to resolve this situation would probably be to bring all your data into one data frame. As this nested list appears to be structured by country and year, it should be possible to create a data frame with the variables country and year that includes all your data. Alternatively, the package ˙purrr˙ provides some very nice tools to work with lists. If you want to apply a function to a deeper level of a nested list then use `purrr::map_depth()`. – shs Sep 12 '19 at 12:54
  • 1
    I am curious of this object, *data frame list "temperatures.coordinates"*. List of data frames? Or a data frame with lists in columns? Please `dput` this object. See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451). – Parfait Sep 12 '19 at 14:10
  • 1
    Also, your code to apply to a single country and month appears to use the entire object *temperatures.coordinates* in `idw`. How do you run *single* country and *single* month in that code block? – Parfait Sep 12 '19 at 14:13
  • temperatures.coordinates looks like this: https://imgur.com/cr3PquB. Its a list of 37 x 12 data frames. For dput, do I post the whole output? The code for single case only uses one data frame with data for one month. Thanks! – Lisa Sep 12 '19 at 14:35

1 Answers1

1

The question in the header of your issue has the following answer:

a <- list(list(1,2), list(3,4))
res <- rapply(a, function(x) x^2)

As suggested in the comments, you need to provide a sample of data to get help with your code.

slava-kohut
  • 4,203
  • 1
  • 7
  • 24