Bug 14618 - is.na method called on raster object returns disordered values
is.na method called on raster object returns disordered values
Status: CLOSED INVALID
Product: R
Classification: Unclassified
Component: Misc
R 2.13.0
All All
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-06-29 20:35 UTC by Ethan Plunkett
Modified: 2014-02-16 11:42 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ethan Plunkett 2011-06-29 20:35:45 UTC
is.na called on a raster object in windows  R (2.13.0) returns a matrix of the right dimensions but with the position of the TRUE and FALSE values shuffled from where they should be.  They are shuffled such that reassembling the resulting matrix with byrow=TRUE argument set in the matrix function yields the correct result. 

Executing the following 6 lines (in windows) reveals the problem:

  R.version
  r <- as.raster(matrix(colors()[1:30], ncol=5, nrow=6))
  r[3,2] <- NA
  is.na(r)  # NA shows up in wrong place ([6,2] )
  is.na(as.matrix(r)) # na shows up in right place [3,2]
  matrix(data=is.na(r), nrow=nrow(r), ncol=ncol(r), byrow=TRUE)


Sample Output

Windows:

> R.version
               _                            
platform       i386-pc-mingw32              
arch           i386                         
os             mingw32                      
system         i386, mingw32                
status                                      
major          2                            
minor          13.0                         
year           2011                         
month          04                           
day            13                           
svn rev        55427                        
language       R                            
version.string R version 2.13.0 (2011-04-13)
> r <- as.raster(matrix(colors()[1:30], ncol=5, nrow=6))
> r[3,2] <- NA
> is.na(r)  # NA shows up in wrong place ([6,2] )
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
[6,] FALSE  TRUE FALSE FALSE FALSE
> is.na(as.matrix(r)) # na shows up in right place [3,2]
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE
[3,] FALSE  TRUE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE
> matrix(data=is.na(r), nrow=nrow(r), ncol=ncol(r), byrow=TRUE) # This also works: 
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE
[3,] FALSE  TRUE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE


Sample output Mac: 
(the raster method for is.na works fine on the mac)


> R.version
               _                            
platform       i386-apple-darwin9.8.0       
arch           i386                         
os             darwin9.8.0                  
system         i386, darwin9.8.0            
status                                      
major          2                            
minor          11.1                         
year           2010                         
month          05                           
day            31                           
svn rev        52157                        
language       R                            
version.string R version 2.11.1 (2010-05-31)
> r <- as.raster(matrix(colors()[1:30], ncol=5, nrow=6))
> r[3,2] <- NA
> is.na(r)  # NA shows up in wrong place ([6,2] )
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE
[3,] FALSE  TRUE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE
> is.na(as.matrix(r)) # na shows up in right place [3,2]
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE FALSE FALSE
[3,] FALSE  TRUE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE
> matrix(data=is.na(r), nrow=nrow(r), ncol=ncol(r), byrow=TRUE) 
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] FALSE FALSE FALSE  TRUE FALSE
[3,] FALSE FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE


Thanks!
Ethan Plunkett
Comment 1 Ben Bolker 2011-07-12 13:01:02 UTC
Fails on 

R version 2.13.0 alpha (2011-03-18 r54865)
Platform: i686-pc-linux-gnu (32-bit)

and

R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

as well.  Are you sure it "works on the Mac", or maybe just on version 2.11.0?
Comment 2 Ethan Plunkett 2011-07-13 14:59:40 UTC
Originally I thought this was system dependent but it appears that it is version dependent instead.  It didn't manifest in version 2.11 on mac but does under 2.13 on windows, mac, and, as reported by Ben Bolker, linux on a mix of 32 and 64 bit systems.
Comment 3 Brian Ripley 2011-09-04 11:54:35 UTC
This is user error: a raster object is not a matrix, and no one said that
is.na applied to it.

Look at unclass(r): it has an NA at element (6,2).
Comment 4 Jackie Rosen 2014-02-16 11:42:47 UTC
(spam comment removed)