4

I'm a complete R beginner, and am trying to do something pretty basic - make histograms of two vectors I imported from Excel.

The vectors are xa and xb. I tried hist(xa), and get the following error:

Error in hist.default(xa) : 'x' must be numeric

So I did some searching, and tried to remedy this using as.numeric(xa), and got:

Error: (list) object cannot be coerced to type 'double'

So I tried the as.list function, but it turned my vector into a matrix. Not really sure what's going on. The numbers in the vectors are all 4 digits between about -2 and +10. Any help would be greatly appreciated!

mnel
  • 113,303
  • 27
  • 265
  • 254
user1705219
  • 41
  • 1
  • 1
  • 2
  • 3
    Welcome to SO, please post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Given that this is a data problem, post the output from `dput(head(xa))` and `dput(head(xb))` – mnel Sep 28 '12 at 05:19

1 Answers1

6

Here's something you can try... no guarantees, since you have not given a working example:

newXa <- sapply(xa, as.numeric)
hist(newXa)

What should be done is to look at the structure of 'x'

str(x)

Then if 'xa' is how you are referring to x[['a']] you would do this:

hist( x[['a']] )

And if str(x) showed that the "a" column were a factor, one might have more success with this:

 hist( as.numeric(as.character(x[['a']]))  )
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    `as.numeric` might have bad effects if a numeric variable has accidentally been converted to a factor ... – Ben Bolker Apr 03 '13 at 02:24