'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
Solution 1:[1]
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)
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrea M |
| Solution 2 | jay.sf |
