[R-sig-Geo] as() and conversion from character to factor

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sun Oct 7 17:26:30 CEST 2012


For now, you should be able to prevent this by starting your script with an

options(stringsAsFactors = FALSE)

but in future releases, sp will not do this conversion.

Thanks,

On 10/04/2012 09:31 PM, MacQueen, Don wrote:
> I just noticed that in the following sequence the final step to
> SpatialGridDataFrame converts the character column to factor, whereas the
> previous ones do not. Source the following little script, and it will
> hopefully show what I'm seeing. Shouldn't this be consistent?
> 
> Thanks
> -Don
> 
> 
> require(sp)
> require(maptools)
> 
> tstdf <- expand.grid(x=1:3, y=1:3)
> tstdf$ch <- rep('a',nrow(tstdf))
> 
> tstsp <- tstdf
> coordinates(tstsp) <- c('x','y')
> proj4string(tstsp) <- CRS('+init=epsg:32610')
> 
> tstpx <- as(tstsp, "SpatialPixelsDataFrame")
> 
> tstgrd <- as(tstpx, "SpatialGridDataFrame")
> 
> print(class(tstdf$ch))
> print(class(tstsp$ch))
> print(class(tstpx$ch))
> print(class(tstgrd$ch))
> 
> 
> #########
> I get:
> 
>> print(class(tstdf$ch))
> [1] "character"
>> print(class(tstsp$ch))
> [1] "character"
>> print(class(tstpx$ch))
> [1] "character"
>> print(class(tstgrd$ch))
> [1] "factor"
> #########
> 
> 
> 
> 
> 
> 
> 
>> sessionInfo()
> R version 2.15.1 (2012-06-22)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
> 
> locale:
> [1] C
> 
> attached base packages:
> [1] stats     utils     datasets  grDevices graphics  methods   base
> 
> other attached packages:
> [1] maptools_0.8-16 lattice_0.20-6  foreign_0.8-50  sp_1.0-0
> 
> loaded via a namespace (and not attached):
> [1] grid_2.15.1 rmacq_1.1-8
> 
> 
> 
> 
> 
> 
> 
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list