Skip to content

K-means clustering with landsat data

3 messages · Vikram Ranga, Mathieu Rajerison, Roger Bivand

#
Hello Everyone,
I am trying to run k-means algorithm with R on Landsat images but it is 
giving error that the data type is not supported.
My syntax is
juneb1<-readGDAL("G:/landsat/New Landsat/June/2002-06-08/8juneb10.tif")
kmenjuneb<-kmeans(juneb1,10,iter.max = 6, nstart = 1,
        algorithm = c("Hartigan-Wong", "Lloyd", "Forgy",
                      "MacQueen"))
error message
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

Is there anyother way to do cluster analysis to spatial data in R?
Any help would be greatly appreciated.


Vikram.
#
On Wed, 11 Jan 2012, Vikram Ranga wrote:

            
Please do check to see what has been read in. Use names(juneb1) to check 
the names of the bands/columns/variables in it. The object is a 
SpatialGridDataFrame, not a numeric matrix, not a data.frame. kmeans() 
takes first argument x:

        x: numeric matrix of data, or an object that can be coerced to
           such a matrix (such as a numeric vector or a data frame with
           all numeric columns).

So:

kmenjuneb<-kmeans(juneb1$band1, centers=10)

may work, if the first name was band1. However, it will not work well on a 
single variable. In addition, it does not work on data with missing 
values, so you may need to say:

df <- as(juneb1, "data.frame")
df1 <- df[complete.cases(df),]

kmenjuneb<-kmeans(df1, centers=10)

or something like that. Beware that subsetting a single column object may 
drop a dimension.

Reading the help pages of readGDAL, SpatialGridDataFrame and kmeans would 
have helped.

Hope this clarifies,

Roger