请问,这个r代码运行后,绘制出来的图形,为啥跟书上的不一样呢?
> set.seed(1234)
> c1<-matrix(rnorm(n,mean=0,sd=.5),ncol=2)
Warning message:
In matrix(rnorm(n, mean = 0, sd = 0.5), ncol = 2) :
data length [65] is not a sub-multiple or multiple of the number of rows [33]
> c2<-matrix(rnorm(n,mean=3,sd=2),ncol=2)
Warning message:
In matrix(rnorm(n, mean = 3, sd = 2), ncol = 2) :
data length [65] is not a sub-multiple or multiple of the number of rows [33]
> mydata<-rbind(c1,c2)
> mydata<-as.data.frame(mydata)
> names(mydata)<-c("x","y")
> with(mydata,plot(x,y,pch=19,main="Scatter Plot with 10000 Observations"))
> with(mydata,plot(x,y,pch=19,main="Scatter Plot with 10,000 Observations"))
>
【以下回答由 GPT 生成】
问题解答:
这段R代码中的问题在于,代码中使用了一个未定义的变量n。因此,在运行代码时会出现错误。为了解决这个问题,你需要确保n的值是正确定义的。
根据代码逻辑,n应该表示数据点的数量。你可以手动定义n的值,或者根据你的数据集自动计算。这取决于你想要绘制图形的数据集大小。
下面是更新后的代码,其中n的值为100(你也可以根据实际需要修改这个值):
set.seed(1234)
n <- 100
c1 <- matrix(rnorm(n,mean=0,sd=.5),ncol=2)
c2 <- matrix(rnorm(n,mean=3,sd=2),ncol=2)
mydata <- rbind(c1,c2)
mydata <- as.data.frame(mydata)
names(mydata) <- c("x","y")
with(mydata, plot(x,y,pch=19,main="Scatter Plot with 100 Observations"))
请注意,我还更新了绘图命令中的标题,使其与实际数据点数量匹配。
如果你的数据集大小与书上的不同,那么你的图形肯定会有所不同。在此代码中,我假设你的数据集大小为100。你可以根据自己的数据集大小进行相应的调整。
希望这个解答对你有帮助。如果你还有其他问题,请随时提问。
【相关推荐】