这是我的代码How to get next number in sequence in R
seqNext <- function(x,n=1) {
L <- length(x)
dat <- data.frame(x=seq_along(x), y=x)
seqGuess <- unname(
predict(lm(y ~ poly(x, 2), data=dat), newdata=list(x=seq(L,L)))
)
seqAnswer <- tail(x, n=1)
print(paste0("guess:",seqGuess," ","answer:",seqAnswer))
if(seqGuess == seqAnswer){
print("Correct")
}
else(
print("Incorrect")
)
}
readData <- function(){
data <- read.csv("numberseries.csv", check.names = F)
data
apply(data,1,seqNext)
}
有问题的数据是一个如下所示的CSV文件:
174,232,290,348,406,464
180,210,240,270,300,330
1920,2016,2112,2208,2304,2400
3132,3190,3248,3306,3364,3422
510,540,570,600,630,660
638,696,754,812,870,928
2494,2552,2610,2668,2726,2784
1230,1260,1290,1320,1350,1380
3648,3744,3840,3936,4032,4128
2250,2280,2310,2340,2370,2400
为什么尽管seqGuess == seqAnswer,但在某些行中却收到了“不正确”的打印值?