我有一个data.frames列表和一个spatial.data.frames列表,它们都有相同的列数和相同的名称。现在,我已经更改了存储在data.frames列表中的(普通) data.frames中的一些列名,并希望将这些更改写入存储在另一个列表(data.frames)中的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的一个列表中的列名应该写为(data.frames)的另一个列表的列名。
到目前为止,我尝试的是:
changeCOL <- function(x, y){
names(y)
}
test<-mapply(changeCOL,x=list_DF,y=listofSpatialDF)该函数成功地读取了不同data.frames的列名,并将它们保存在相应的名称下。但现在我不知道如何继续或解决这个问题。
发布于 2015-09-14 18:34:35
你有一个正确的想法--我稍微改变了你的功能,现在它应该能用了:
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 TRUEhttps://stackoverflow.com/questions/32569387
复制相似问题