The colon in
i-19:i
has higher precedence than the subtraction, so for i=20, you get 20 - c(19,20) rather than (20-19):20 as you were expecting. Suitably placed brackets solve this
data<-rnorm(100,mean= 3,sd=1.8)
reg.curve<-rep(0,length(data)-20)
x<-seq(1:20)
for(i in 20:length(data)){
reg<-lm(data[(i-19):i]~x)
reg.curve[i]<-tail(fitted(reg),1)
}