Well, sticking to your loop that you seem comfortable with try this: ## list to hold dataframes extracList <- vector("list", length(1981:2010))
cnt <- 0 for(i in 1981:2010){ cnt <- cnt + 1 rasterObj <-get( c(paste("RR_", i, "_1", sep=""))) extractList[[cnt]]<- extract(rasterObj, coords, df=T)} } See ?get which is kind of the opposite of assign. To coerce the list of dataframes into one dataframe several methods are available: do.call("rbind", extractList) plyr::rbind.fill(extractList) plyr::ldply(extractList, data.frame) as.data.frame(data.table::rbindlist(extractList)) This was taken from http://stackoverflow.com/questions/2851327/converting-a-list-of-data-frames-into-one-data-frame-in-r where you get get some pros and cons on the methods. The fastest way will probably be to create a huge dataframe and during the loop calculated the row indices of that dataframe for each raster into the result from extract should be put. Yours sincerely / Med venlig hilsen Frede Aakmann Tøgersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 fr...@vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender. > -----Original Message----- > From: Beatriz R. Gonzalez Dominguez [mailto:aguitatie...@hotmail.com] > Sent: 21. april 2014 16:27 > To: Frede Aakmann Tøgersen; r-help@r-project.org > Subject: Re: [R] Loop to extract from variables in the workspace > > Hi Frede, > > Many thanks for your reply. > 1. The first argument in extract is a Formal class RasterLayer in the > Workspace (e.g RR_1981_1 ). > > 2. I created an intermediate name to hold the result fromthe extract > function because I'd like to create several dataframes with the output > of the iterative (loop) extraction. I'd like to get the same result as > when I do: > > PE.coords_01_1981 <- extract(RR_1981_1, coords, df=T) > PE.coords_01_1982 <- extract(RR_1982_1, coords, df=T) > PE.coords_01_1983 <- extract(RR_1983_1, coords, df=T) > PE.coords_01_1984 <- extract(RR_1984_1, coords, df=T) > [... this works no problem] > > 3. 'coords' is a SpatialPointsDataFrame. > > 4. I used assign in the loop becuase I thought it was the way forward to > create new variables out of it. Isn't it? > > What I'd like to do is to use coordinate points ('coords') to extract > raster pixel values (eg. 'RR_1981_1') on which the points are overlying. > Then I'd like to build a bigger data frame including the data from all > the outputs (i.e. PE.coords_01_1981, PE.coords_01_1982). > > Hope to have explained myself properly. Please let me know if anything > else should be clarified. > > Best wishes, > > Bea > > > On 21/04/2014 15:17, Frede Aakmann Tøgersen wrote: > > Hi Beatriz > > > > Did you read the help for extract{raster} carefully? > > > > Several things can be wrong. > > > > 1) First argument to extract is not a file name but a raster object. > > 2) In the loop you name an object extract as an intermediate name to hold > the result from the extract function. Do you think there could be a name > clash? R is clever but perhaps not clever enough. > > 3) coords are of the right class (see ?extract). > > 4) assign can be useful from time to time. But in a loop? > > > > I think the things you are doing are some intermediate results that needs > more processing. Do you think this is the right way to do that. For instance > instead of storing the immediate result as separate objects why not store > those in a list. > > > > Perhaps if you tell us what you would like to do overall, i.e. from first > > to > last, then we will be able to help you to become more efficient. > > > > > > Yours sincerely / Med venlig hilsen > > > > > > Frede Aakmann Tøgersen > > Specialist, M.Sc., Ph.D. > > Plant Performance & Modeling > > > > Technology & Service Solutions > > T +45 9730 5135 > > M +45 2547 6050 > > fr...@vestas.com > > http://www.vestas.com > > > > Company reg. name: Vestas Wind Systems A/S > > This e-mail is subject to our e-mail disclaimer statement. > > Please refer to www.vestas.com/legal/notice > > If you have received this e-mail in error please contact the sender. > > > > > >> -----Original Message----- > >> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] > >> On Behalf Of Beatriz R. Gonzalez Dominguez > >> Sent: 21. april 2014 14:53 > >> To: r-help@r-project.org > >> Subject: [R] Loop to extract from variables in the workspace > >> > >> Dear all, > >> > >> I'm starting to work with loops and I'm stucked on something. > >> I've been searching and trying different possibilities but I don't get > >> to the solution. > >> I'd be very grateful if you could share any ideas that you think may help. > >> > >> library("raster") > >> > >> # All my variables are in the workspace > >> > >> # This is what I'd like to obtain, but with a loop (I'm working with > >> several years and variables). > >> PE.coords_01_1981 <- extract(RR_1981_1, coords, df=T) > >> PE.coords_01_1982 <- extract(RR_1982_1, coords, df=T) > >> PE.coords_01_1983 <- extract(RR_1983_1, coords, df=T) > >> PE.coords_01_1984 <- extract(RR_1984_1, coords, df=T) > >> PE.coords_01_1985 <- extract(RR_1985_1, coords, df=T) > >> PE.coords_01_1986 <- extract(RR_1986_1, coords, df=T) > >> PE.coords_01_1987 <- extract(RR_1987_1, coords, df=T) > >> PE.coords_01_1988 <- extract(RR_1988_1, coords, df=T) > >> PE.coords_01_1989 <- extract(RR_1989_1, coords, df=T) > >> PE.coords_01_1990 <- extract(RR_1990_1, coords, df=T) > >> > >> > >> # This is one of the things I've tried. > >> > >> for(i in 1981:2010){ > >> file <- c(paste("RR_", i, "_1", sep="")) > >> extract <- extract(file, coords, df=T)} > >> names.a <- paste("PE.coords_01_", i, sep="") > >> assign(names.a, value=extract) > >> } > >> > >> # I get the following error. > >> Error in (function (classes, fdef, mtable) : > >> unable to find an inherited method for function 'extract' > >> for signature '"character", "SpatialPointsDataFrame"' > >> # I think the error must be something when I'm defining 'file' > >> > >> Thanks a lot for any help! > >> > >> ______________________________________________ > >> R-help@r-project.org mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide http://www.R-project.org/posting- > >> guide.html > >> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.