Skip to content
Prev 339818 / 398500 Next

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

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