[R-sig-Geo] map algebra with ascii grid

Tim Keitt tkeitt at gmail.com
Mon Jul 16 02:21:03 CEST 2007


On 7/14/07, Roger Bivand <Roger.Bivand at nhh.no> wrote:
> On Sat, 14 Jul 2007, Michal Gallay wrote:
>
> > Dear R Users,
> >
> > could you please advise me on doing map algebra with spatial grids? It's
> > the first time I am using spatial objects in R. I have imported an ascii
> > grid file and wanted to round the values in it or sum with a value, but it
> > gives an error message:
> >
> >> require(maptools)
> >> x <- readAsciiGrid(fname="xxxx.asc")
> >> x+3
> > Error in x + 3 : non-numeric argument to binary operator
> >> round(x,2)
> > Error in round(x, digits) : Non-numeric argument to mathematical function
> >
>
> Arithmetic operations are not defined for SpatialGridDataFrame operations,
> as the columns of the data frame (here a single column) may contain data
> of different classes. Do the operations on the columns directly:
>
> names(x)
>
> If there is a column called "band1", then
>
> x$band1 + 3
>
> will print "band1" + 3, and
>
> x$band1 <- x$band1 + 3
>
> will add 3 to the "band1" column. Think what would happen if band1 was
> categorical or logical to see why doing arithmetic directly isn't such a
> good idea.

Roger,

While I agree that arithmetic directly on S?DF objects does not make a
lot of sense, I have always taken the view that if the name of a type
includes "data frame", then you are implicitly promising that the
object is substitutable for any other data.frame class object in a
function call. So if S?DF is meant to inherit (formally or informally)
from data.frame, one should be able to use a S?DF anywhere you could
use an ordinary data.frame object without changing the result. Of
course, I realize that S?DF is not mean to inherit directly from
data.frame (so it is clear from the programming standpoint, ie there
is no "inherits" relationship between data.frame and S?DF). Perhaps it
would be more clear to users if a different naming scheme were used?

THK

>
> Hope this helps,
>
> Roger
>
> > Thank you for any suggestions.
> >
> > Best wishes,
> >
> > Michal
> >
> >
> >
> >
>
> --
> Roger Bivand
> Economic Geography Section, Department of Economics, Norwegian School of
> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand at nhh.no
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


-- 
Timothy H. Keitt, University of Texas at Austin
Contact info and schedule at http://www.keittlab.org/tkeitt/
Reprints at http://www.keittlab.org/tkeitt/papers/
ODF attachment? See http://www.openoffice.org/




More information about the R-sig-Geo mailing list