21601℃

# R if循环里老是missing value where TRUE/FALSE needed

create<- function(x,y)
{
layer <- raster(n4)
layer2 <- raster(n3)
for(c in 1:x)
{
for(r in 1:y)
{
if(layer[c,r]==0)
{
nl<- layer2
nl[c,r]<-NA
writeRaster(nl, “cloud.tif”)
}
else
{ layer[c,r]=layer2[c,r]}
}
}
}create(10,10)

setwd(“E:/Landsat-data-NASA atm-corrected/sample_day1”)
restdir2 <- (“E:/Landsat-data-NASA atm-corrected/sample_day1″)
n3 <- list.files(restdir2, pattern=”*band4_clip_1.tif”, full.names=TRUE)
n4 <- list.files(restdir2, pattern=”*cloud_qa_clip_1.tif”, full.names=TRUE)

x=10;y=10;
layer <- raster(n4)
layer2 <- raster(n3)
for(c in 1:x)
{
for(r in 1:y)
{
if(layer[c,r]==0)
{
nl<- layer2
nl[c,r]<-NA
writeRaster(nl, “cloud.tif”)
}
else
{ layer[c,r]=layer2[c,r]}
}
}

> a=NA;if(a==0){}
Error in if (a == 0) { : missing value where TRUE/FALSE needed

·你好 我现在是想处理两张地图

> x <- data.frame(x1=c(0,NA,1,2,3,4,0,5),x2=c(0,NA,NA,2,0,4,0,5))
> x
x1 x2
1  0  0
2 NA NA
3  1 NA
4  2  2
5  3  0
6  4  4
7  0  0
8  5  5
> # 将x按列转换为向量，找到所有值为0的序号
> list <- which(x==0)
> list
[1]  1  7  9 13 15
> # 所有值为0的序号转换为横纵坐标
> axes <- as.matrix(data.frame(x = list %% dim(x)[1], y = list %/% dim(x)[1] + 1))
> axes
x y
[1,] 1 1
[2,] 7 1
[3,] 1 2
[4,] 5 2
[5,] 7 2
> #检验这些坐标代表的数据值是否为0
> x[axes]
[1] 0 0 0 0 0

• 在线沟通，请点我在线咨询

• 咨询热线：
13261671783
客服qq：
3285157825