我是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")
答案 0 :(得分:0)
问题是你使用的是不同长度的矢量。 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)