[R] density(x)

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jul 5 16:57:31 CEST 2004


Christoph Hanck wrote:
> Hello and thanks for your reply
> 
> Hopefully, my answer arrives at the correct place like that (if not,
> I am sorry for bothering you, but please let me know...)
> 
> To sum up my procedure (sp is exactly the same thing as spr, I had
> just tinkered with the names while trying sth. to solve this problem)
> 
>> sp<-read.table("c:/ratsdata/sp3.txt", col.names="sp") 
>> xd<-density(sp)
> 
> Error in density(sp) : argument must be numeric
> 
> The suggested remedies yield the following
> 
>> str(sp)
> 
> `data.frame':   195 obs. of  1 variable: $ sp: int  11 10 10 12 25 22
> 12 23 13 15 ...
> 
>> xd<-density(as.numeric(sp))
> 
> Error in as.double.default(sp) : (list) object cannot be coerced to
> double

It is telling you that it cannot convert a list into a numeric object. A 
data frame is a list so it is telling you that you cannot convert the 
data frame into a numeric vector.

> Hence, it does not seem to be a factor. Declaring it as numeric gives
> another error message, on which I haven't yet found any help in
> Google/the archive.

You want the sp column of the data frame sp not the data frame sp itself 
(perhaps you should choose a name for the data frame that is different 
to a column name)

 > sp <- data.frame(sp = rnorm(100))
 > density(sp)
Error in density(sp) : argument must be numeric
 > density(sp$sp)

Call:
         density(x = sp$sp)

Data: sp$sp (100 obs.); Bandwidth 'bw' = 0.3007

        x                  y
  Min.   :-3.37457   Min.   :0.0001983
  1st Qu.:-1.73138   1st Qu.:0.0389884
  Median :-0.08819   Median :0.1157180
  Mean   :-0.08819   Mean   :0.1519886
  3rd Qu.: 1.55500   3rd Qu.:0.2227940
  Max.   : 3.19818   Max.   :0.4766640

Does this help?

with(sp, density(sp)) would also do what you want, see ?with, and there 
are other ways.

Gavin

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpson at ucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list