I thought it was over. I think I simplified my problem and something really weird is happening to my data.
I am actually working with a SpatialPolygonsDataFrame and I am trying to replace the data.frame inside of the object (object@data).
so the beginning is an example I got from someone else:
################ data ################
library(sp)
grd <- GridTopology(c(1,1), c(1,1), c(3,3))
polys <- as.SpatialPolygons.GridTopology(grd)
centroids <- getSpPPolygonsLabptSlots(polys)
x <- centroids[,1]
y <- centroids[,2]
z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
map <- SpatialPolygonsDataFrame(polys,
data=data.frame(x=x, y=y, z=z, row.names=getSpPPolygonsIDSlots(polys)))
########## my case ##############
new.data1<-data.frame(x=x, y=y, z=z*2, row.names=getSpPPolygonsIDSlots(polys))
new.data2<-data.frame(x=x, y=y, z=z/2, row.names=getSpPPolygonsIDSlots(polys))
names<-c("new.data1","new.data2")
maps<-c("map1","map2")
for(i in 1:length(names)){
assign(maps[i],map) ### I copy the map in each of the maps I want
### and here I try to copy the new data.frames in the data slot of my maps
assign(maps[i],`@<-`(get(maps[i]),"data",get(names[i])))
}
#### check
identical(get(maps[1]),get(maps[2]))
I thought everything was ok, but instead, I got that the two data.frames in the different maps are the same. I got a bit lost in what I am saying in the row:
assign(maps[i],@<-
(get(maps[i]),"data",get(names[i])))
and I think that here should be the problem. why does this last line assign the same data.frame (the last names[i]) to both new maps (map1 & map2) ????
many thanks