[R-sig-Geo] Errore in sprintf(fmt, ...) ?

massimodisasha massimodisasha at yahoo.it
Sat Dec 16 01:52:56 CET 2006


>>>> Hi,
>>>> in the last days i had an error during the computation of a  
>>>> morfological clustering on a dtm,
>>>> thanks to R.Bivand and his suggestions i've resolved it.
>>>>
>>>> error was generated by a large presence of Nas ( the sea  
>>>> surface ) on my scene:
>>>> http://img299.imageshack.us/img299/1619/immagine8gg6.png
>>>>
>>>> The problem is firstly that there are NAs in morph, and then
>>>> that the string listing the row numbers of the NAs exceeds 8K  
>>>> characters
>>>> (my earlier message : Errore in sprintf(fmt, ...) : La lunghezza
>>>> della stringa eccede la dimensione del buffer di 8192)
>>>>
>>>> So the right procedure is to try clara on morph once you have  
>>>> omitted the NAs.
>>>> If the NAs are for all variables in a row, coercing from  
>>>> SpatialGridDataFrame to
>>>> SpatialPixelsDataFrame will work,
>>>> if only some, we need to set the whole row to NA, coerce,  
>>>> cluster, add the clara results to the
>>>> SpatialPixelsDataFrame, coerce back to SpatialGridDataFrame,  
>>>> then back to
>>>> GRASS.
>>>>
>>>>
>>>> ##My system configuration:
>>>>
>>>> R version 2.4.0 (2006-10-03)
>>>> powerpc-apple-darwin8.7.0
>>>> locale:
>>>> C
>>>> attached base packages:
>>>> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"      
>>>> "datasets"
>>>> [7] "base"
>>>> other attached packages:
>>>> spgrass6    rgdal maptools  foreign       sp  cluster
>>>> "0.3-4"  "0.5-3"  "0.6-5" "0.8-18"  "0.9-4" "1.11.4"
>>>>
>>>> ##This is the log of previous error:
>>>>
>>>> ...
>>>> ...
>>>> names(morph) <- c 
>>>> ("er","crosc","longc","slope_n","profc","minic","maxic")
>>>> morph.clara <- clara(morph, k=5, stand=F)
>>>> Errore in clara(morph, k = 5, stand = F) :
>>>> Observations
>>>> 1929241,1931311,1933381,1935451,1937521,1939591,1941661,1943731,194 
>>>> 58
>>>> 01,1947871,........................................................ 
>>>> ..
>>>> .............44521,2146591,2148661,2150731,2152801,2154871,2156941
>>>> ,2159011,2161081,2163151,2165221,2167291,2169361,2171431
>>>> traceback()
>>>> 2: stop(ngettext(length(i), sprintf("Observation %d
>>>> has", i[1]),
>>>>        sprintf("Observations %s have", paste(i,
>>>> collapse = ","))),
>>>>        " *only* NAs --> omit for clustering")
>>>> 1: clara(morph, k = 5, stand = F)
>>>>
>>>>
>>>>
>>>>
>>>> ##this code fix the problem:
>>>>
>>>>
>>>> 	library(spgrass6)
>>>> 	x <- readRAST6(c ('$map_1,..,$map_N'))
>>>> 	save(x, file="geomorphological.RData", compress=TRUE)
>>>> 	rm(x)
>>>> 	gc()
>>>> 	
>>>> 	
>>>>         #   copies the GridTopology object from its slot in x,  
>>>> to use later on;  it is the current GRASS region
>>>> 	
>>>> 	grd <- x at grid
>>>> 	
>>>> 	#   Sets or retrieves projection attributes on classes  
>>>> extending  SpatialData
>>>> 	#   again copying from x so as to allow x to be removed from the
>>>> 	#   workspace. It read the projection info and apply it to the  
>>>> x data	
>>>> 		
>>>> 	proj <- x at proj4string
>>>> 	
>>>> 	#   copying the data slot (in R >= 2.4.0 a data frame) out to  
>>>> xx; for some
>>>> 	#   reason getting it in other ways made a copy of it, so this  
>>>> saves space.
>>>> 	
>>>> 	xx <- x at data
>>>> 	
>>>> 	rm(x)
>>>> 	gc()
>>>> 	library(cluster)
>>>> 	
>>>> 	#   makes a logical vector saying which rows are all NAs  
>>>> (FALSE), and
>>>> 	#   which are not (TRUE).
>>>> 	
>>>> 	NArow <- apply(xx, 1, function(x) !(all(is.na(x))))
>>>> 	
>>>> 	#   create xx1 object (my data without Nas)
>>>> 	#   selects the rows for which NArow is TRUE.
>>>> 	
>>>> 	xx1 <- xx[NArow,]
>>>> 	
>>>> 	rm(xx)
>>>> 	gc()
>>>> 	res <- clara(xx1, k='$clusters', metric="'$metric'",  
>>>> stand='$stand', sampsize='$sampsizes', trace='$trace',  
>>>> samples='$samples',
>>>> 	medoids='$medoids', keep.data='$keepdata', rngR='$rngR')
>>>> 	gc()
>>>> 	rm(xx1)
>>>> 	gc()
>>>> 	
>>>> 	#   object name out_class, an integer vector of NAs the same  
>>>> length as NArow.
>>>> 	
>>>> 	out_class <- as.integer(rep(NA, length(NArow)))
>>>> 	
>>>> 	#   Inserts the results of res$clustering - c() removes the  
>>>> unecessary names -
>>>> 	#   into the elements of out_class for which NArow is TRUE (so  
>>>> the elements
>>>> 	#   corresponding to all NA inputs are NA in the output).	
>>>> 	
>>>> 	out_class[NArow] <- c(res$clustering)
>>>> 	
>>>> 	#   out is a SpatialGridDataFrame, with the same region as x,  
>>>> same projection
>>>> 	#   as x, but only the classification output (correctly  
>>>> assigned to elements
>>>> 	#   with regard to NAs in the input) in its data slot.
>>>> 	
>>>> 	out <- SpatialGridDataFrame(grd,  
>>>> proj4string=proj,data=data.frame(cls=out_class))
>>>> 	
>>>>
>>>> 	save(out, file="klust_out.RData", compress=TRUE)
>>>> 	library(rgdal)
>>>> 	writeGDAL(out, fname="clust.tif", drivername="GTiff", type="Byte")
>>>> 	gc()
>>>>
>>>> i hope this is usefool
>>>>
>>>> regards,
>>>> Massimo




More information about the R-sig-Geo mailing list