<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi Danilo, <br>
<br>
the issue here are NAs in the input data, which the kmeans algorithm
can't deal with.<br>
If you do it "by hand" you'll need to exclude NAs (see example
below). <br>
Note that in practice, it is recommendable to use
RStoolbox::unsuperClass(), which takes care of NA handling and will
also work if your raster is too big to fit into memory.<br>
<br>
<br>
## Example: K-Means with NAs<br>
library(raster)<br>
data(lsat)<br>
<br>
## setup empty output raster<br>
output <- raster(lsat)<br>
output[] <- NA<br>
<br>
## load all values into matrix<br>
values <- lsat[]<br>
<br>
## Identify pixels which have valid values in all bands<br>
valid <- complete.cases(values)<br>
<br>
## Cluster only complete pixels<br>
km <- kmeans(values[valid,], centers = 5)<br>
<br>
## Turn matrix into raster<br>
output[valid] <- km$cluster<br>
plot(output)<br>
<br>
<br>
Best regards,<br>
Benjamin<br>
<br>
<div class="moz-cite-prefix">On 31.10.19 18:04, Danilo Procaccini
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAPO-vgUM=4FeFznHB9Sao5EEnQKJTsv+9DQrfbvFZdSDBgYUng@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div>Dear RS-ecology community,</div>
<div>
<div>
<div id="gmail-m_-7350267937432995941gmail-tw-target">
<div id="gmail-m_-7350267937432995941gmail-kAz1tf">
<div
id="gmail-m_-7350267937432995941gmail-tw-target-text-container">
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left" dir="ltr"><span lang="en">I am practicing with the exercises proposed in the book.
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">At page 178
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en"><span lang="en">launching the command</span></span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">p224_r63_2011.kmeans <- kmeans (p224r63_2011_sub[], centers =5, iter.max = 100, nstart = 10)
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">i receive the error
Error in do_one(nmeth) :
NA/NaN/Inf <code><span class="gmail-kwd">in</span><span class="gmail-pln"> foreign </span><span class="gmail-kwd">function</span><span class="gmail-pln"> call</span></code> (arg 1)
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">I loaded the raster from data_book/raster_data/final/p224r63_2011.grd with the command brick as shown in the book
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">and then i launched the command p224r63_2011_sub <- p224r63_2011[[c(1:4]]
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">any solutions?
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">Best regards,
</span></pre>
<pre id="gmail-m_-7350267937432995941gmail-tw-target-text" style="text-align:left"><span lang="en">Danilo Procaccini</span></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
RS-ecology mailing list
<a class="moz-txt-link-abbreviated" href="mailto:RS-ecology@lists.uni-wuerzburg.de">RS-ecology@lists.uni-wuerzburg.de</a>
<a class="moz-txt-link-freetext" href="https://lists.uni-wuerzburg.de/mailman/listinfo/rs-ecology">https://lists.uni-wuerzburg.de/mailman/listinfo/rs-ecology</a>
</pre>
</blockquote>
<br>
</body>
</html>