[R] mat2listw function

David Winsemius dwinsemius at comcast.net
Sat Nov 30 17:50:48 CET 2013

On Nov 30, 2013, at 1:43 AM, Jim Lemon wrote:

> On 11/30/2013 07:05 AM, Jacquelyn Pless wrote:
>> Hi all,
>> I am attempting to create a weights object and perform a Moran I test as
>> well. I have a very large spatial weights matrix (roughly 22,000x22,000)
>> that was created in Excel and read into R, and I'm now trying to implement:
>> library(spdep)
>> SW=mat2listw(matrix)
>> I am getting the following error:
>> Error in if (any(x<0)) stop ("values in x cannot be negative"): missing
>> value where TRUE/FALSE needed.
>> What's going wrong here? My current matrix is all 0's and 1's, with no
>> missing values and no negative elements. What am I missing?
>> I'd appreciate any advice. Thanks in advance for your help!
> Hi Jacquelyn,
> The first thing I would do is enter:
> sum(is.na(x))
> and if anything other than zero is returned, you will know what's wrong. Your dataset has been imported from Excel, and that application is notorious for exporting things in the way it knows you want them to be. Not.

The second thing to do (since I wonder if an NA value would provoke that error) is to run 


I'm guessing it's not a matrix and perhaps a dataframe. In which case then run:

lapply(matrix, class) to see if the columns are "numeric". I'm guessing they may be "factor".

David Winsemius
Alameda, CA, USA

More information about the R-help mailing list