Pergunta

I have been trying to do something like:

get(names[i])$column1<-vector

or more precisely in my case, get(names[i])@data<-data.frame

I have been struggling with get(), assign() searching for similar things ... but cannot figure out how to do it

example:

names<-c("york","paris","donostia")
vector<- 1:8
data<- as.data.frame(matrix(rep(0,9),ncol=3))

How could I do this?

Foi útil?

Solução

Let's start with your example data.frame:

data <- as.data.frame(matrix(rep(0,9),ncol=3))

> data
  V1 V2 V3
1  0  0  0
2  0  0  0
3  0  0  0

If I understand your question, you want to add a column, but you want to do so referring to your data.frame by a variable that holds its name.

The syntax df$foo <- x is a convenience for applying a function named $<- that takes the data.frame df, a column name, and a new column and returns a new data.frame. The magic part is that it captures the return value back in original the variable df.

With that in mind, here's what I think you're after:

name <- 'data'
assign(name, `$<-`(get(name), 'new.column', letters[1:3]))

> data
  V1 V2 V3 new.column
1  0  0  0          a
2  0  0  0          b
3  0  0  0          c

Outras dicas

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

Would code along these lines do the trick?

maps <- list()
for (name in ['city1', 'city2', ...etc...]) {
  data <- getDataForThisParticularMap(name)
  maps <- append(maps, list(SpatialPolygonsDataFrame(...etc..., data=data)))
}

This is what you want, if your overall goal is a list of different map objects.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top