Evasparkling
Evasparkling

Reputation: 1

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

I'm trying to plot a histogram in R but I get the following: Error in hist.default(data) : 'x' must be numeric

I'm using the function hist(data). Can anyone help me resolve the issue?

Please see the attachment below:enter image description here

Upvotes: 0

Views: 2953

Answers (2)

jay.sf
jay.sf

Reputation: 73262

Looks to me as if you made a mistake when reading in the data. read.csv should work. (BTW, height and weight appear to be confused in your data!)

dat <- read.csv('./unit_3_test_data')

hist(dat$height)
hist(dat$weight)
hist(dat)

Data:

n <- 50
set.seed(42)
tmp <- data.frame(
  height=rnorm(n, 180, 20),
  weight=rnorm(n, 70, 3)
)
write.csv(tmp, 'unit_3_test_data', row.names=F, quote=F)

Upvotes: 0

Andrea M
Andrea M

Reputation: 2462

hist expects a numeric vector. If you use hist(data), hist gets the whole dataset and doesn't know what to do with it.

You should use the $ operator to get a single column from that dataset.

hist(data$height)
hist(data$weight)

Upvotes: 0

Related Questions