[R] hist(list)?

Bill Venables wvenable at arcola.stats.adelaide.edu.au
Thu Mar 18 02:59:20 CET 1999


> It seems that the following should work:

This is a common misapprehension.  Actually it should not work
for a simple, clear but very commonly misunderstood reason.

> 
> > x <- read.table('1A.tab',header=T,skip=1);
> > mode(x[1])
> [1] "list"
> > x[1]
> 	   p01
> R1-00 0.0295
> R1-01 0.0283
> R1-02 0.0145
> R1-03 0.0235
> R1-04 0.0339
> R1-05 0.0239
> R1-06 0.0414
> R1-07 0.0259
> > hist(x[1])
> Error: hist: x must be numeric
> > as.numeric(x[1])
> Error: (list) object cannot be coerced to vector type 14

The problem is that x[1] is not the first component, it is a
sub-list consisting of the first component alone.  If you want
the first *component* you have a couple of choices here,
including

x[[1]], x[["p01"]], x[, 1], x$p01 or x[, "p01"]

but not x[1].

> I've worked around the problem by removing the is.numeric()
> test from src/library/base/R/hist.R and in happily produces a
> histogram for me.

In my view modifying standard system functions is always very bad
idea, but I suppose you're the boss on your machine....

> Is there a better way of letting R know that it is working with
> a list of numerics only?

unlist() is designed to flatten lists into vector objects.

-- 
_________________________________________________________________
Bill Venables, Department of Statistics,    Tel.: +61 8 8303 3026
The University of Adelaide,                 Fax.: +61 8 8303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables at adelaide.edu.au

*PLEASE NOTE* From 1 May 1999 my contact details will change to:
-----------------------------------------------------------------
Bill Venables, Statistician, CMIS Environmetrics Project.

Physical address:                            Postal address:
CSIRO Marine Laboratories,                   PO Box 120,       
233 Middle St, Cleveland, Queensland         Cleveland, Qld, 4163
AUSTRALIA                                    AUSTRALIA

Telephone: +61 7 3826 7200     Email: Bill.Venables at cmis.csiro.au     
      Fax: +61 7 3826 7304
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list