[R] Loop to extract from variables in the workspace

Beatriz R. Gonzalez Dominguez aguitatierra at hotmail.com
Mon Apr 21 14:53:12 CEST 2014

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.


# 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!

More information about the R-help mailing list