0

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

  • Could you provide your data with `dput()`, not a photo? – YH Jang Apr 22 '22 at 14:02
  • **Note:** Please read our tutorial on how to make a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) before posting next time. Cheers! – jay.sf Apr 22 '22 at 18:17

2 Answers2

0

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)
Andrea M
  • 2,314
  • 1
  • 9
  • 27
0

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)
jay.sf
  • 60,139
  • 8
  • 53
  • 110