D, I think the correct answer has been given but it may be hard to decipher. The below might do what you want (not tested; as you provide no example data which would have been easy to include starting from the examples in 'extract')
for(j in 1:3) { polys <- get(paste0('SpatiallPolygonsDataFrame_', j, 'km')) for(k in 1:17){ out <- paste0('extract_', j, '_', k, 'km') assign(out, extract(stack, polys, layer=[k], nl=1, df=TRUE)) } } However, it is VERY inefficient to call extract separately for each layer in a RasterStack. In stead you could do something like this: res <- list() for(j in 1:3) { polys <- get(paste0('SpatiallPolygonsDataFrame_', j, 'km')) e <- extract(stack, polys, layer=1, nl=17, df=TRUE) names(e) <- paste0('extract_', j, '_', 1:17, 'km') res <- c(res, e) } # you could add the 1:17 loop after extract to do the assignments, but # in the example below you get a list of data.frames which is much more convenient # A simpler approach might be: polys <- list(a, b, c) res <- list() for(j in 1:3) { e <- extract(stack, polys[[j]], layer=1, nl=17, df=TRUE) names(e) <- paste0('extract_', j, '_', 1:17, 'km') res <- c(res, e) } Best, RH On Sun, Dec 16, 2012 at 2:18 PM, dcd <dennis.d...@gmail.com> wrote: > As asked here > < > http://stackoverflow.com/questions/13897455/pass-variable-name-to-a-function-and-output-inside-of-loop-in-r > > > on stackoverflow: > > > for(j in 1:3) { > for(k in 1:17){ > extract_[j]km <- extract(RasterStack, > SpatialPolygonsDataFrame_[j]km, > layer=[k], nl=1, df=TRUE) > } > } > > While the Rasterstack remains constant, the SpatialPolygon and layer within > the RasterStack will iterate. From the posts I've read I'm now getting the > hint that loops and R aren't the best combination, but what approach would > achieve something similar to the above pseudo-loop code? I've looked at > get, paste, and lapply, but I can't get them to work with extract. Any help > would be most appreciated. > > Many thanks in advance! > > D > > > > > > > -- > View this message in context: > http://r-sig-geo.2731867.n2.nabble.com/raster-How-to-loop-extract-function-tp7581978.html > Sent from the R-sig-geo mailing list archive at Nabble.com. > > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo