[RS-ecology] error in kmeans statement during manual classification

Benjamin Leutner benjamin.leutner at uni-wuerzburg.de
Tue Sep 13 10:21:32 CEST 2016


kmeans can't deal with NAs.
This is one of the issues unsuperClass takes care of for you.
Here's an example how you could deal with it:

library(RStoolbox)
library(raster)

## Example data with NAs
data(lsat)
lsat[c(5,10)] <- NA

## Convert raster to matrix and check for NAs
## Warning: this step will load the complete raster into memory
ldata  <- lsat[]
valid  <- complete.cases(ldata)

km  <- kmeans(ldata[valid,], 3)

## Create empty output raster
map   <- raster(lsat)
map[] <- NA

## Fill valid pixels
map[valid] <- km$cluster

On 09.09.2016 18:32, Natalia Quinteros wrote:
> Hi all,
>
> I don't understand the meaning of this error, if somebody can explain 
> it to me I'll be very grateful.  :)
>
> >p224r63_2011_sub <- p224r63_2011[[c(1:4)]] > p224r63_2011.kmeans <- 
> kmeans(p224r63_2011_sub[], centers = 5, iter.max = 100, nstart = 10) 
> Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
>
> Best,
>
> Natalia
>
>
> _______________________________________________
> RS-ecology mailing list
> RS-ecology at lists.uni-wuerzburg.de
> https://lists.uni-wuerzburg.de/mailman/listinfo/rs-ecology


-- 
Benjamin Leutner M.Sc.
PhD candidate

Department of Remote Sensing
University of Wuerzburg
Campus Hubland Nord 86
97074 Wuerzburg, Germany

Tel: +49-(0)931-31 89594
Email: benjamin.leutner at uni-wuerzburg.de
Web: http://www.fernerkundung.uni-wuerzburg.de



More information about the RS-ecology mailing list