madieke
madieke

Reputation: 21

R cannot use hist() because "content not numeric" due to negative decimal numbers?

I am new to R and I am trying to draw a histogram using hist() of a list of 100,000 numbers like this

-0.764
-0.662
-0.764
-0.019
0.464
0.668
0.464

but I cannot do it because R complains that the content is not numeric. This is what I've tried:

I think their might be a problem with the decimal point "." or the negative signs "-" or both. Any ideas?

Many thanks!

Upvotes: 2

Views: 2066

Answers (2)

juba
juba

Reputation: 49033

R seems to have transformed the first column of your data as a factor. This should not happen if all your data in this column where numeric in your file. So there must be an element which is not recognized as a number.

You can try the following (which is a bit dirty) in R to try to identify where the problem is. Starting with the following factor :

R> v <- factor(c("0.51", "-0.12", "0.345", "0.45b", "-0.8"))

You can identify which value causes problem with :

R> v[is.na(as.numeric(as.character(v)))]
[1] 0.45b

And you can find the position of this value in your vector with :

R> which(is.na(as.numeric(as.character(v))))
[1] 4

Upvotes: 2

Wilduck
Wilduck

Reputation: 14126

If you want to convert a factor to a numeric type, you have to understand how factors work.

Internally, each distinct item (each "factor") in a column of class factor is stored as a number. These are the numbers that you're seeing when you run as.numeric. These numbers, are actually just indexes on the levels of the factor, so if you type levels(t[,1]) you should see a list of all the different values in the first column of your data frame.

So, with this knowledge, we can use a trick to extract the actual numbers:

as.numeric(levels(t[,1])[t[,1]])

Of course, if R interpreted this row of numbers as a factor when read.table was reading it, before this trick will work, you'll have to remove the row that contains the non-numeric type.

Upvotes: 1

Related Questions