Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R X需要是一个数值_R - Fatal编程技术网

R X需要是一个数值

R X需要是一个数值,r,R,我有一个统计课的作业,我从老师那里复制了代码,但是我一直得到一个错误,x应该是数字,我不知道问题在哪里 Survey=read.csv("ClassSurvey.csv", na.strings = c("", " ", "NA")) attach(Survey) library(FSAdata) op = par(oma=c(0,0,1.5,0), mar=c(3,3,2,1)) hist(Height ~ Gender,

我有一个统计课的作业,我从老师那里复制了代码,但是我一直得到一个错误,x应该是数字,我不知道问题在哪里

Survey=read.csv("ClassSurvey.csv", na.strings = c("",          " ",   "NA"))
attach(Survey)
library(FSAdata)                  

op = par(oma=c(0,0,1.5,0), mar=c(3,3,2,1))
hist(Height ~ Gender,
     las=1,
     nrow=2, ncol=1,
     cex.main=0.9, cex.lab=0.8, cex.axis=0.8, 
     mgp=c(1.8,0.6,0), 
     xlab = "Height (cms)")

谢谢

你有几个问题。首先,不能对非数值变量绘制直方图。试试这个:

data(ToothGrowth)
str(ToothGrowth)
'data.frame':   60 obs. of  3 variables:
 $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
 $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
 $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...

attach(ToothGrowth)
尝试绘制
supp
的直方图,该直方图是“factor”类变量(不是数字)

第二个问题是
hist
函数不接受公式
y~x
。所以

hist(len~supp) 
即使
len
是数值,也会失败。不过,这是可行的:

hist(len) 

我想知道你是不是把老师的密码抄错了?还是老师的代码不正确?或者别的什么

您好,请通过
getwd()
检查路径,如果文件不在那里,请通过
setwd()
设置路径,然后使用
read.csv()
希望这会有帮助。您可以指定代码要执行的操作吗?每个性别的身高直方图?你的公式
Height~Gender
建议x轴应该是Gender,但是你的
xlab
参数将是“Height”,所以有点不对劲。
hist(len)