将 data.frames 列表中的 data.frames 的列名称分配给 R 中 data.frames 列表中的其他(空间)data.frames

Assign column names of data.frames in a list of data.frames to other (Spatial) data.frames in a list of data.frames in R

我有一个 data.frames 列表和一个空间列表。data.frames 都有相同的列数和相同的名称。现在我已经更改了存储在 data.frames 列表中的 (Normal)data.frames 中的一些列名称,并想将这些更改写入存储在另一个列表中的 data.frames 中,即 (Spatial) data.frames.

我怎样才能存档这样的东西?

一些例子:

require (sp)
mtcars.S <-mtcars
coordinates(mtcars.S) <- c('gear', 'carb')
mtcars.S$coords.x1 <- 12345 
mtcars.S$coords.x2 <- 12345

attitude.S <- attitude
coordinates(attitude.S) <- c('critical', 'advance')
attitude.S$coords.x1 <- 12345 
attitude.S$coords.x2 <- 12345

quakes.S <- quakes
coordinates(quakes.S) <- c('lat', 'long')
quakes.S$coords.x1 <- 12345 
quakes.S$coords.x2 <- 12345


f.Names <- c('mtcars.S','attitude.S','quakes.S')

listofSpatialDF <- mget(f.Names)

b2DF <- function(x) {
  as.data.frame(x)
}

list_DF <- lapply(listofSpatialDF,b2DF)

coordsD <- function(x){
  x[,!names(x) %in% c("coords.x1","coords.x2")]  
}

list_DF <- lapply(list_DF,coordsD)

然后在data.frames中更改了一些列名称。 data.frames 的一个列表中的列名应写为 (Spatial)data.frames.

的另一个列表的列名

到目前为止我尝试的是:

changeCOL <- function(x, y){
  names(y)

}

test<-mapply(changeCOL,x=list_DF,y=listofSpatialDF)

这个函数设法读出不同 data.frames 的列名,并将它们保存在相应的名称下。但是现在我不知道如何继续或解决这个问题。

你的想法是正确的 - 我稍微改变了你的功能,它现在应该可以工作了:

changeCOL <- function(x, y){
  names(y) <- names(x)
  return(y)
}

test<-mapply(changeCOL,x=list_DF,y=listofSpatialDF)

# Test to show the names are the same now
names(test[[1]])==names(list_DF[[1]])
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE