从列表中重复元素的子集对

时间:2016-01-21 19:52:30

标签: r string list raster

我正在尝试按年重复列出一个列表。如果文件具有相同的名称,这可以很容易地解决,但事实并非如此。

列表如下:

 dput(list_raster)
    c("F101992.v4b_web.stable_lights.avg_vis.tif", "F101993.v4b_web.stable_lights.avg_vis.tif", 
    "F101994.v4b_web.stable_lights.avg_vis.tif", "F121994.v4b_web.stable_lights.avg_vis.tif", 
    "F121995.v4b_web.stable_lights.avg_vis.tif", "F121996.v4b_web.stable_lights.avg_vis.tif", 
    "F121997.v4b_web.stable_lights.avg_vis.tif", "F121998.v4b_web.stable_lights.avg_vis.tif", 
    "F121999.v4b_web.stable_lights.avg_vis.tif", "F141997.v4b_web.stable_lights.avg_vis.tif", 
    "F141998.v4b_web.stable_lights.avg_vis.tif", "F141999.v4b_web.stable_lights.avg_vis.tif", 
    "F142000.v4b_web.stable_lights.avg_vis.tif", "F142001.v4b_web.stable_lights.avg_vis.tif", 
    "F142002.v4b_web.stable_lights.avg_vis.tif", "F142003.v4b_web.stable_lights.avg_vis.tif", 
    "F152000.v4b_web.stable_lights.avg_vis.tif", "F152001.v4b_web.stable_lights.avg_vis.tif", 
    "F152002.v4b_web.stable_lights.avg_vis.tif", "F152003.v4b_web.stable_lights.avg_vis.tif", 
    "F152004.v4b_web.stable_lights.avg_vis.tif", "F152005.v4b_web.stable_lights.avg_vis.tif", 
    "F152006.v4b_web.stable_lights.avg_vis.tif", "F152007.v4b_web.stable_lights.avg_vis.tif", 
    "F152008.v4b_web.stable_lights.avg_vis.tif", "F162004.v4b_web.stable_lights.avg_vis.tif", 
    "F162005.v4b_web.stable_lights.avg_vis.tif", "F162006.v4b_web.stable_lights.avg_vis.tif", 
    "F162007.v4b_web.stable_lights.avg_vis.tif", "F162008.v4b_web.stable_lights.avg_vis.tif", 
    "F162009.v4b_web.stable_lights.avg_vis.tif", "F182010.v4d_web.stable_lights.avg_vis.tif", 
    "F182011.v4c_web.stable_lights.avg_vis.tif", "F182012.v4c_web.stable_lights.avg_vis.tif", 
    "F182013.v4c_web.stable_lights.avg_vis.tif")

正如您所看到的,有几个年份文件重复(i.e. 1994, 1997-1999, 2000-2003, etc.)。我的想法是使用raster::overlay函数构建一个包含重复年份文件对的列表,以表示它们的值。我尝试过以下方法:

    library(stringr)  
    #Create a list of only the years of the files
    year_list <- lapply(list_raster, str_sub, 4, 7)
    #Select the duplicate years
    duplicated_years <- year_list[duplicated(year_list)]
    #Select the duplicate file-years
    duplicated_rasters <- list_raster[lapply(list_raster, str_sub, 4, 7) %in% duplicated_years]
    #Make all the pairs between files
    duplicated_rasters <- combn(duplicated_rasters, 2, simplify=T)

尽管如此,我现在有一个包含很多对的矩阵,而且我很难选择重复的年份。有一个简单的方法吗?我认为我的经验不足使我&#34; overcode&#34;这个。

1 个答案:

答案 0 :(得分:1)

您可以在此处使用by按年份应用函数(overlay in occurence)。当然,您应该首先从文件名中提取年份:

 by(xx,INDICES=substr(xx,4,7),FUN = function(x)## function body)

其中xx是您的文件列表:

  dput(head(xx))
c("F101992.v4b_web.stable_lights.avg_vis.tif", "F101993.v4b_web.stable_lights.avg_vis.tif", 
"F101994.v4b_web.stable_lights.avg_vis.tif", "F121994.v4b_web.stable_lights.avg_vis.tif", 
"F121995.v4b_web.stable_lights.avg_vis.tif", "F121996.v4b_web.stable_lights.avg_vis.tif"
)