我有一个光栅文件列表,我想浏览每个文件,如果有一个NA值,我想从列表中删除它!
喜欢这个列表
[1] "./2013105_33UXP_04_05_L8_sr_band2.tif" "./2013105_33UXP_04_05_L8_sr_band3.tif" "./2013105_33UXP_04_05_L8_sr_band4.tif"
[4] "./2013105_33UXP_04_05_L8_sr_band5.tif" "./2013105_33UXP_04_05_L8_sr_band6.tif" "./2013105_33UXP_04_05_L8_sr_band7.tif"
[7] "./2013114_33UXP_04_05_L8_sr_band2.tif" "./2013114_33UXP_04_05_L8_sr_band3.tif" "./2013114_33UXP_04_05_L8_sr_band4.tif"
[10] "./2013114_33UXP_04_05_L8_sr_band5.tif" "./2013114_33UXP_04_05_L8_sr_band6.tif" "./2013114_33UXP_04_05_L8_sr_band7.tif"
[13] "./2013121_33UXP_04_05_L8_sr_band2.tif" "./2013121_33UXP_04_05_L8_sr_band3.tif" "./2013121_33UXP_04_05_L8_sr_band4.tif"
[16] "./2013121_33UXP_04_05_L8_sr_band5.tif" "./2013121_33UXP_04_05_L8_sr_band6.tif" "./2013121_33UXP_04_05_L8_sr_band7.tif"
我怎么做?
感谢
答案 0 :(得分:2)
这是一个可重现的解决方案示例
library(raster)
# example data
r <- raster(ncol=10, nrow=10)
set.seed(0)
# 10 layers
s <- stack(lapply(1:10, function(i) setValues(r, runif(ncell(r)))))
# set about half the values to NA
s[s < .5] <- NA
s
#class : RasterBrick
#dimensions : 10, 10, 100, 10 (nrow, ncol, ncell, nlayers)
#resolution : 36, 18 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
#data source : in memory
#names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10
#min values : 0.5186343, 0.5004410, 0.5069395, 0.5070356, 0.5008505, 0.5253055, 0.5017548, 0.5161239, 0.5055311, 0.5019486
#max values : 0.9919061, 0.9926841, 0.9815635, 0.9960774, 0.9937492, 0.9959655, 0.9756573, 0.9994554, 0.9906600, 0.9999306
现在使用示例数据删除超过50%的NA
# count the NA values in each layer
i <- cellStats(is.na(s), sum)
# fraction that is NA
i <- i/ncell(s)
i
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9 layer.10
# 0.52 0.46 0.62 0.56 0.53 0.44 0.46 0.51 0.55 0.54
# select the layers that more than half the cells with values
ss <- s[[which(i>.5)]]
ss
#class : RasterBrick
#dimensions : 10, 10, 100, 7 (nrow, ncol, ncell, nlayers)
#resolution : 36, 18 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
#data source : in memory
#names : layer.1, layer.3, layer.4, layer.5, layer.8, layer.9, layer.10
#min values : 0.5186343, 0.5069395, 0.5070356, 0.5008505, 0.5161239, 0.5055311, 0.5019486
#max values : 0.9919061, 0.9815635, 0.9960774, 0.9937492, 0.9994554, 0.9906600, 0.9999306
答案 1 :(得分:-1)
如果我们能够获得可重现的代码,显示当您将其中一个光栅文件读入R时的样子,那将会有所帮助。它是否作为数据框出现?作为清单?作为矩阵?这使得很难完全回答你的问题。
我想你会想要的东西......
library(dplyr)
library(raster)
new_list_of_files = list()
for (file in seq_along(list_of_files)){
imported_raster = raster(file)
df <- as.data.frame(imported_raster)
#check NAs
p_cent_na <- sum(is.na(df))/(nrow(df)*ncol(df))
if (p_cent_na > .9){
df[is.na(df),] <- 0
new_list_of_files <- list(new_list_of_files, df)
}
}
我不知道这是否有效,因为我需要一个你的.tif文件的例子。