José Joel. José Joel. - 3 years ago 225
R Question

Can't draw Histogram, 'x' must be numeric

I have a data file with this format:

Weight Industry Type

251,787 Kellogg h

253,9601 Kellogg a

256,0758 Kellogg h


I read the data and try to draw an histogram with this commands:

ce= read.table("file.txt", header= T)

we = ce[,1]
in = ce[,2]
ty = ce[,3]


But I get this error:
Error en hist.default(we) : 'x' must be numeric.

What do I need to do in order to draw histograms for my three variables ?

Answer Source

Because of the thousand separator, the data will have been read as 'non-numeric'. So you need to convert it:

 we <- gsub(",", "", we)   # remove comma
 we <- as.numeric(we)      # turn into numbers

and now you can do


and other numeric operations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download