'x' 尝试绘制直方图时必须是数字错误

'x' must be numeric error when trying to plot a histogram

我正在尝试绘制直方图。然而,即使所有的值看起来都是数字或 NA,当我尝试 运行 hist() 它仍然是 returns 错误。任何帮助将不胜感激。

corruption <- read.csv("Corruption.csv")
corruption[ corruption == "-" ] <- NA
hist(corruption$X2015)

我怀疑这与“-”字符的存在有关。当我使用 table(corruption$X2015) 时,这是输出:

 - 11 12 15 16 17 18 19 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 
 0  1  1  2  2  3  4  1  3  3  1  1  6  3  6  7  4  2  5  5  4  4  4  7  5  7  4  1  2  3  5  1 
46 47 49 50 51 52 53 54 55 56 58 60 61 62 63 65 70 71 74 75 76 77 79  8 81 83 85 86 87 88 89 90 
 2  2  1  1  4  2  3  1  4  3  1  1  3  2  2  1  4  1  1  3  2  1  2  2  3  1  1  1  2  1  1  1 
91 
 1 

X2015 转换为数字,这会自动将非数字更改为 NA

corruption$X2015 <- as.numeric(as.character(corruption$X2015))

然后您可以使用 hist

hist(corruption$X2015)