Jonathan Tian Jonathan Tian - 1 year ago 54
R Question

R in counting data

Right now I'm trying to do a bell curve on a file called output9.csv on my.

Here is my code, I want to uses z score to detect outliers, and uses the difference between the va

hist(height) #histogram

pop_sd <- sd(height)*sqrt((length(height)-1)/(length(height)))
pop_mean <- mean(height)

But I have this error after I tried the histogram part,


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

how should I fix this?

Answer Source

Since I don't have your data I can only guess. Can you provide it? Or at least a portion of it?

What class is your data? You can use class(data) to find out. The most common way is to have table-like data in data.frames. To subset one of your columns to use it for the hist you can use the $ operator. Be sure you subset on a column that actually exists. You can use names(data) (if data is a data.frame) to find out what columns exist in your data. Use nrow(data) to find out how many rows there are in your data.

After extracting your height you can go further. First check that your height object is numeric and has something in it. You can use class(height) to find out.

Did you try to convert it to numeric? as.numeric(height) might do the trick. as.numeric() can coerce all things that are stored as characters but might also be numbers automatically. Try as.numeric("3") as an example.

Here an example I made up.

height <- c(1,1,2,3,1)
# [1] "numeric"


This works just fine, because the data is numeric.

In the following the data are numbers but formatted as characters.

height_char <- c("1","1","2","3","1")
# [1] "character"

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

So you have to coerce it first:


..and then it works fine.

For future questions: Try to give Minimal, Complete, and Verifiable Examples.