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

Time:04-23

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

CodePudding user response:

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)

CodePudding user response:

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)
  • Related