我是R的新手,还处于学习阶段。在绘制直方图和频率多边形代码时,我遇到错误。我无法调试它。请帮助理解错误。
#HISTOGRAM
x=seq(200,1200,by=200)
width=200
x
freq=c(6,16,24,20,10)
freq
lowerbound=x-(width/2)
upperbound=x+(width/2)
lowerbound
upperbound
lowerbound[1]
brks=c(lowerbound[1],upperbound)
brks
y = rep(x,freq) #getting error Error in rep(x, freq) : invalid 'times' argument
hist(y,breaks = brks,xlab = "Monthly Rent",ylab = "Families",main = "Histogram)")
#Frequency Polygon
x=seq(200,1200,by=200)
width=200
x
freq=c(6,16,24,20,10)
freq
x1=c(0,x,1400)
x1
f1=c(0,freq)
f1
plot(x1,f1)
#Error: Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
#plot(x1,f1,"b",xlab="Rent",ylab = "Families",main="FP")
发布于 2017-08-06 00:37:49
问题是你使用的是不同长度的向量。rep
和plot
都希望这两个输入向量具有相同的项目数。
试试这个,看看我在哪里做了修改:
x=seq(200,1200,by=200)
width=200
x
#freq=c(6,16,24,20,10)
freq=c(6,16,24,20,10, 5) # Added one more item, 5
freq
lowerbound=x-(width/2)
upperbound=x+(width/2)
lowerbound
upperbound
lowerbound[1]
brks=c(lowerbound[1],upperbound)
brks
y = rep(x,freq) # No more errors here
hist(y, breaks = brks,xlab = "Monthly Rent",ylab = "Families",main = "Histogram")
x=seq(200,1200,by=200)
x
#freq=c(6,16,24,20,10)
freq=c(6,16,24,20,10,5,3) # Added two more items, 5 and 3
freq
x1=c(0,x,1400)
x1
f1=c(0,freq)
f1
plot(x1,f1)
https://stackoverflow.com/questions/45523972
复制相似问题