[R] conversion error from numeric to factor in raster: Error in 1:ncol(r) : argument of length 0, r command: as.factor()

Stefan Schmidt stefan.schmidt at ufz.de
Wed May 7 11:31:59 CEST 2014


Hello,

thanks a lot for the fast response and the hint! Actually, it helped to 
already integrate as.factor in the generation of the raster. This is 
also working for uploading or diverse other raster functionalities like 
projection, e.g. as.factor(raster("C:\\...\\r.tiff")) or 
as.factor(projectRaster(r, to, method='ngb'))

Furthermore I found in another blog (Dec 2013), following:

This [the as.factor(raster)] is a bug in package raster.

Once again, thanks a lot!

Best,

Stefan


Am 06.05.2014 20:27, schrieb David L Carlson:
> Does
>
> values(r) <- as.factor(1:ncell(r))
>
> do what you want?
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> ----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Stefan Schmidt
> Sent: Tuesday, May 6, 2014 11:22 AM
> To: r-help at r-project.org
> Subject: [R] conversion error from numeric to factor in raster: Error in 1:ncol(r) : argument of length 0, r command: as.factor()
>
> Hello together,
>
> I was wondering how I can solve the following conversion problem of a
> raster file: when I try to convert the values from the raster (r) from
> numeric into a factor via as.factor(r) always the error appears: "Error
> in 1:ncol(r) : argument of length 0".
>
> r <- raster(ncol=5, nrow=5)
> values(r) <- 1:ncell(r)
> as.factor(r)
>
> Urgently I have to figure out how to convert a numeric raster into a
> factor raster for a predict() calculation within the raster package.
>
> Every hint is very welcome!
>
> Best,
>
> Stefan
>

-- 
********************************************************************
Stefan Schmidt

Abteilung Landschaftsökologie/
Department Computational Landscape Ecology
Helmholtz-Zentrum für Umweltforschung GmbH – UFZ/
Helmholtz Centre for Environmental Research – UFZ
Permoserstraße 15 / 04318 / Leipzig
Phone: +49 341 235 - 1056
Fax: +49 341 235 - 1939
Email: stefan.schmidt at ufz.de
WWW: http://www.ufz.de

Sitz der Gesellschaft: Leipzig
Registergericht: Amtsgericht Leipzig, Handelsregister Nr. B 4703
Vorsitzender des Aufsichtsrats: MinDirig Wilfried Kraus
Wissenschaftlicher Geschäftsführer: Prof. Dr. Georg Teutsch
Administrative Geschäftsführerin: Dr. Heike Graßmann



More information about the R-help mailing list