[R-sig-Geo] K-means clustering with landsat data

Roger Bivand Roger.Bivand at nhh.no
Wed Jan 11 11:46:36 CET 2012


On Wed, 11 Jan 2012, Vikram Ranga wrote:

> 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")

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


> 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.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list