我想从数据框中创建直方图,但是每次使用代码时都会出错'x' must be numeric。
df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120),
col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 104, 114, 134))
hist(df)
Run Code Online (Sandbox Code Playgroud)
你可以做
hist(df$col1)
Run Code Online (Sandbox Code Playgroud)
要么
with(df, hist(col2))
Run Code Online (Sandbox Code Playgroud)
如果您希望所有列都在各自的直方图中,则可以执行以下操作
par(mfrow=c(2,1))
histout=apply(df,2,hist)
Run Code Online (Sandbox Code Playgroud)