将矩阵列表的列值粘贴到另一个矩阵列表中

时间:2014-10-20 14:05:50

标签: r matrix paste

我有一个shapefile列表。以下是其中一个shapefile的示例。

ID Area_ORG LU_1990 LU_2000 CHLU_90_00 LU_2005 CHLU_00_05 Tile UNIQ_ID AREA D_90_00 0 597 27.27 11 11 1111 0 1112 S01_E031 S01_E031_597 274408.8 100 1 622 24.75 11 11 1111 0 1112 S01_E031 S01_E031_622 249063.6 100 2 816 8.97 11 11 1111 0 1112 S01_E031 S01_E031_816 90260.9 100 3 818 20.94 11 11 1111 0 1112 S01_E031 S01_E031_818 210709.6 100 4 886 25.92 11 11 1111 0 1112 S01_E031 S01_E031_886 260828.1 100 5 898 102.33 11 11 1111 0 1112 S01_E031 S01_E031_898 1029736.2 100 6 935 41.13 11 11 1111 0 1112 S01_E031 S01_E031_935 413891.1 100 7 1139 27.09 11 11 1111 0 1130 S01_E031 S01_E031_1139 272603.0 100 8 1169 22.95 11 11 1111 0 1112 S01_E031 S01_E031_1169 230945.5 100 9 1192 44.91 11 11 1111 0 1112 S01_E031 S01_E031_1192 451941.1 100 10 1196 26.91 11 11 1111 0 1112 S01_E031 S01_E031_1196 270798.2 100 11 1203 27.72 11 11 1111 0 1112 S01_E031 S01_E031_1203 278951.3 100 12 1244 38.61 11 11 1111 0 1112 S01_E031 S01_E031_1244 388546.4 100 13 1435 10.44 11 11 1111 0 1130 S01_E031 S01_E031_1435 105058.9 100 14 1436 24.75 11 12 1112 0 1230 S01_E031 S01_E031_1436 249060.9 520 15 1437 18.63 11 12 1112 0 1230 S01_E031 S01_E031_1437 187476.3 520

每个shapefile都有一个Tile列。在这种情况下,瓦片列在列8中>但有时它在其他专栏中。所以我想通过使用Tile名称来编写表。我在下面尝试了这个代码,但它不起作用。

for (i in 1:length(list_shp_Tanzania)){ write.csv(list_shp_Tanzania[[i]], file = paste(list_shp_Tanzania[[i]]@data$Tile, "csv", sep = "."), sep = ",", row.names = FALSE) }

1 个答案:

答案 0 :(得分:0)

尝试

res <-  Map(function(x,y,z) {x[,z] <- y[,z];x},
               list_matrix_Tanzania_Mod, LU_Mod2000, columnsToTransfer1)

sapply(res, function(x) x[,"A"])[1:3,] #changed list matrix
#     [,1] [,2] [,3] [,4]
#[1,]   19   10   19   15
#[2,]   19   15    3   17
#[3,]    6   19   20    8

sapply(list_matrix_Tanzania_Mod, function(x) x[,"A"])[1:3,] #original list matrix
#     [,1] [,2] [,3] [,4]
#[1,]   15   24   27   47
#[2,]   12   19    7   26
#[3,]   36   14   26   33

sapply(LU_Mod2000, function(x) x[,"A"])[1:3,] #second list matrix `A` columns
#     [,1] [,2] [,3] [,4]
#[1,]   19   10   19   15
#[2,]   19   15    3   17
#[3,]    6   19   20    8

更新

如果在任何一个列表的某些列表元素中找不到columnsToTransfer1的情况。例如,通过删除list_matrix_Tanzania_Mod

的第2个元素中感兴趣的列
 list_matrix_Tanzania_Mod[[2]] <- list_matrix_Tanzania_Mod[[2]][,-1]
  Map(function(x,y,z) {x[,z] <- y[,z];x},
             list_matrix_Tanzania_Mod, LU_Mod2000, columnsToTransfer1)
  #Error in `[<-`(`*tmp*`, , z, value = c(10L, 15L, 19L, 6L, 10L, 19L, 20L,  : 
  #subscript out of bounds

您可以通过对具有所有相应元素列的列表进行子集化来更正它

   indx <- sapply(list_matrix_Tanzania_Mod, function(x) 
                      any(colnames(x)==columnsToTransfer1))

   indx1 <- sapply(LU_Mod2000, function(x) any(colnames(x)==columnsToTransfer1)) 
   indx2 <- indx1&indx
   res <- list_matrix_Tanzania_Mod
   res[indx2] <- Map(function(x,y,z) {x[,z] <- y[,z];x},
        list_matrix_Tanzania_Mod[indx2], LU_Mod2000[indx2], columnsToTransfer1)

数据

columnsToTransfer1 <- "A"
set.seed(24)
list_matrix_Tanzania_Mod <- lapply(1:4, function(i) matrix(sample(1:50, 4*10,
              replace=TRUE), ncol=4, dimnames=list(NULL, LETTERS[1:4])))

set.seed(42)
LU_Mod2000 <- lapply(1:4, function(i) matrix(sample(1:20,10, replace=TRUE),
       ncol=1, dimnames=list(NULL, LETTERS[1])))