[R-br] Função com vários if e else if que não funciona - O RETORNO
Éder Comunello
comunello.eder em gmail.com
Quarta Fevereiro 3 20:56:06 BRST 2016
Senhores, segue o código rodando. O objeto "band" redundante foi alterado
pra "ver".
########################################################################
require(raster); require(sp)
# RasterLayer inventado
r <- raster(nrows=10, ncols=10)
r <- setValues(r, 1:ncell(r))
plot(r)
band2<- as(r, 'SpatialGridDataFrame') ### transforma em
SpatialGridDataFrame
##Função para conversão DN para radiância - LISS III
---------------------------
radconvL<-function(x, band = 2)
{
BAND <- c("2", "3", "4", "5")
ver <- pmatch(band, BAND)
if (is.na(ver))
stop("invalid band")
Lmin = 0
Qmax = 127
x <- as.vector(as.matrix(x))
results <- x
if(band == 2) {
Lmax <- 120.64
} else if(band == 3) {
Lmax <- 151.31
} else if(band == 4) {
Lmax <- 157.57
} else if(band == 5) {
Lmax <- 69.03
}
x <- Lmin + ((Lmax-Lmin)*x)/Qmax
if (class(results) == "SpatialGridDataFrame")
results em data[, 1] <- x
else if (is.data.frame(x))
results <- data.frame(matrix(x, nrow = nrow(results),
ncol = ncol(results)))
else results <- x
print(paste(band, Lmax))
print(results)
return(results)
}
######--
teste2<-radconvL(band2, band = 2)
teste2<-radconvL(band2, band = 3)
================================================
Éder Comunello
PhD Student in Agricultural Systems Engineering (USP/Esalq)
Brazilian Agricultural Research Corporation (Embrapa)
Dourados, MS, Brazil [22 16.5'S, 54 49.0'W]
Em 3 de fevereiro de 2016 19:48, Éder Comunello <comunello.eder em gmail.com>
escreveu:
> Senhores, boa noite!
>
> O problema é que você sobrescreve "band" com o resultado de pmatch (1:4).
> Pode observar que mesmo rodando com band 3:5 o resultado tá errado.
>
> Inclua um print(band) antes do if pra verificar.
>
>
> ================================================
> Éder Comunello
> PhD Student in Agricultural Systems Engineering (USP/Esalq)
> Brazilian Agricultural Research Corporation (Embrapa)
> Dourados, MS, Brazil [22 16.5'S, 54 49.0'W]
>
>
>
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20160203/276a8600/attachment.html>
Mais detalhes sobre a lista de discussão R-br