2014-10-16 111 views
0

我已經創建了這個腳本來計算兩個常量rc_Mod2000_LC,y = S01_E031_FC的一個輸出。如何用多個shapefile數據集創建for循環函數?

# Extract LUC in Mod2000 
LU_S01_E031_FC_Mod2000 <- extract(x=rc_Mod2000_LC, y=S01_E031_FC) 
maj <- function(x){ 
    y <- as.numeric(names(which.max(table(x)))) 
    return(y) 
} 
LU_S01_E031_FC_Mod2000 <- extract(x=rc_Mod2000_LC, y=S01_E031_FC, fun=maj) 
colnames (LU_S01_E031_FC_Mod2000) <- c("LU_2000") 

現在,我想創建一個for循環函數,其中rc_Mod2000_LC是一個常數(光柵),但其中S01_E031_FC是一個變量(shape文件)。 rc_Mod2000_LC是全球土地覆蓋地圖,S01_E031_FC是地塊。我有61個shapefile的列表。

[[1]] 
class  : SpatialPolygonsDataFrame 
features : 16 
extent  : 30.95493, 31.02964, -1.040257, -0.9624111 (xmin, xmax, ymin, ymax) 
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
variables : 16 
names  : ID, Area_ORG, LU_1990, LU_2000, CHLU_90_00, LU_2005, CHLU_00_05,  Tile,  UNIQ_ID,  AREA, D_90_00, D_00_05, Sour_90_00, Sour_00_05, Conf_90_00, ... 
min values : 1139, 10.44,  11,  11,  1111,  0,  1112, S01_E031, S01_E031_1139, 90260.9,  100,  200, GLSE_00, GLSE_05,   3, ... 
max values : 935,  8.97,  11,  12,  1112,  0,  1230, S01_E031, S01_E031_935, 1029736.2,  520,  520, GLSE_00, GLSE_05,   3, ... 

[[2]] 
class  : SpatialPolygonsDataFrame 
features : 2 
extent  : 30.95484, 30.99854, -2.022452, -1.971676 (xmin, xmax, ymin, ymax) 
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
variables : 16 
names  : ID, Area_ORG, LU_1990, LU_2000, CHLU_90_00, LU_2005, CHLU_00_05,  Tile,  UNIQ_ID,  AREA, D_90_00, D_00_05, Sour_90_00, Sour_00_05, Conf_90_00, ... 
min values : 466,  0.06,  11,  11,  1111,  0,  1130, S02_E031, S02_E031_466, 603.7435,  100,  520, GLSE_00,  GE_06,   3, ... 
max values : 975,  3.15,  11,  11,  1111,  0,  1130, S02_E031, S02_E031_975, 31698.1260,  100,  520, GLSE_00, GLSE_05,   3, ... 

因此我想x = rc_Mod2000_LC和y = shapefile的列表。希望有人能幫助我,讓我知道你是否需要澄清。

回答

0

我會做這樣的事情,首先你提取功能:

extract_shape(y=shape,x=rc_Mod2000_LC){ 
    S01_E031_FC<- readShapePoly(shape) 
    proj4string(S01_E031_FC) <- "+proj=longlat +datum=WGS84" 
    extract(y=S01_E031_FC, fun=maj) 
} 

然後你遍歷你塑造名稱:

lapply(list_shapes,extract_shape) 

其中:

list_shapes <- c("S01_E031_FC","S01_E032_FC","S01_E033_FC",...) 

或者,如果更好你的形狀名稱有一定的模式,你可以使用:

list_shapes <- ls(pattern='S1_.*_FC') 
+0

所以,extract_shape是一個函數。所以我應該寫extract_shape <-function(y = shape,x = rc_Mod2000_LC),對吧?而y = shape應該是list_shapes還是不是?謝謝 – 2014-10-16 11:38:41

+0

沒有「y」是列表名稱的元素... – agstudy 2014-10-16 12:41:14

+0

我已更新我的問題,因爲最終您的答案似乎無法解決。 – 2014-10-16 14:10:43