[R-sig-Geo] error "not a numeric vector" when use function "localG"

Torleif Markussen Lunde torleif.lunde at cih.uib.no
Thu Mar 12 22:57:49 CET 2009


And if none of these helps remember to specify the NA (not available) values 
in your data 

read.table("data.txt", na.strings="yournavalues", as.is=TRUE)

If your NA values in the original data was for example "NN" R would think the 
column was all made of factors and thereby convert it. By using the as.is 
argument R will not try to anything smart, and you would than afterwords have 
to specify all columns as factors, numeric etc.

However, if you have given your NA values this should not be a problem. Try to 
make a txt file from the data below, and run the following code to get a 
better grip on what is happening when reading with read.table. Note the dot 
in row 5, column x6

date group x1 x2 x3 x4 x5 x6
02/20/1902 0 7 2.53 168.19 77.79 97.16 17.56
05/01/1920 0 5 2.87 175.25 57.83 157.14 18.73
05/22/1919 0 3 2.79 180.49 84.18 122.12 18.88
07/05/1902 0 7 2.38 165.98 58.74 96.51 9.35
12/03/1902 0 5 2.36 172.72 95.48 147 .
01/10/1914 0 6 2.69 180.33 89.13 131.49 16.88
03/13/1916 0 10 2.8 179.07 68.94 117.72 16.84
03/02/1915 0 10 3.27 170.17 78.69 159.61 26.41

ex1 <- read.table("asisex.txt", head=T, sep=" ", as.is=TRUE)
ex2 <- read.table("asisex.txt", head=T, sep=" ")
ex3<- read.table("asisex.txt", head=T, sep=" ", na.strings=".")
str(ex1)
str(ex2)
str(ex3)

Best wishes
Torleif

On Thursday 12 March 2009 10:16:01 pm Roger Bivand wrote:
> On Thu, 12 Mar 2009, Huang Juying wrote:
> > Dear list members,
> >
> > I am a new R user and spdep user. I tried to use function "localG" to
> > calculate Getis Gi. My targeted variable is log(SalePrice) for over 1000
> > houses. The listw is the number of neighbors within 3000m.
> >
> >> SalePrice.Gi <- localG(SalePrice, nb3000m.W, zero.policy=TRUE)
> >
> > Then I got this error:? SalePrice is not a numeric vector
>
> When you don't know how the object of interest actually got represented in 
> the workspace, try str(), here str(SalePrice). It may be that this is a
> factor (because the decimal sign wasn't what you thought, as Marcelino
> already suggested). It may also be that you did SalePrice <- read.csv(),
> in which case it is a data frame, with column(s). If so, look for its
> names(), and use the one you need, such as SalePrice$var1. The str()
> digest method is very powerful for browsing objects, and has extra
> arguments when the output is too voluminous.
>
> Roger
>
> > I don't understand why. All SalePrice data is numeric, with 6 decimal
> > precision. I tried to import SalePrice from .dat or .txt or .csv file,
> > but got the same error. I tried to play with the header and column/row
> > argument, still no use.
> >
> > I am really frustrated. I guess there is a simple solution, but I just
> > cannot find it!!! Please help me! Thank you in advance!
> >
> > Christina
> > USC Geography department
> >
> >
> >
> >
> >
> > 	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list