Skip to content
Prev 13853 / 29559 Next

K-means clustering with landsat data

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