[R] basic hist() question

Gavin Simpson gavin.simpson at ucl.ac.uk
Sat Aug 21 12:02:33 CEST 2010


On Sat, 2010-08-21 at 11:37 +0200, Adel ESSAFI wrote:
> Hi list
> 
> I loaded the content  of a file dureetasks.txt to variable a. This file
> contains an interger per line.
> when I print a vector, it displays correctly.
> however, when I  try to print the histogram, I get this error message
> 
> > a=read.table("dureetasks.txt")
> > hist(a)
> Error in hist.default(a) : 'x' must be numeric
> 
> Can you help please?

class(a)

str(a)

> a <- data.frame(x = rnorm(100))
> hist(a)
Error in hist.default(a) : 'x' must be numeric

So I suspect a is a data frame with a single column/variable. You need
to pass hist the vector from /inside/ a that you want to plot:

> with(a, hist(x))

is one way to do that. Note that 'x' is used because that is the name of
the variable (component really) in a that I want to plot:

> names(a)
[1] "x"

You will need to work out what the correct name is for the component you
want to plot. If there was no header to your text file then IIRC the
variable will be named X1, but run names(a) and see what it returns.

Because we don't have your dureetasks.txt file, there is no way for me
to clarify that this is the problem and nothing else. If you can't send
us data at the very least use tools like str(a) and class(a), and
head(a) to show us what the data look like.

HTH

G

> 
> regards
> 
> 
> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list