mr.T Asked:2020-11-12 01:49:11 +0000 UTC2020-11-12 01:49:11 +0000 UTC 2020-11-12 01:49:11 +0000 UTC 如何将这条线延伸到未来 772 我们有一个向量 vect <- c(1,3,7,4,12,8,14,9,21) plot(vect,t="l") 选择两个点并在它们之间画一条线 i <- c(3,5) points(i,vect[i],lwd=2,col=4) segments(3,vect[3],5,vect[5],col=2,lwd=2,lty=3) 问题:如何将这条线延伸到未来 n 个值 并将这条线作为带有值的向量 r 1 个回答 Voted Best Answer mr.T 2020-11-12T19:55:32Z2020-11-12T19:55:32Z 写了这个函数 fu <- function(vec,x1,x2){ p.ln <- x2-x1+1 my.trend <- approx(vec[c(x1,x2)],n = p.ln)$y my.trend <- c(rep(NA,x1-1),my.trend) fst <- tail(diff(my.trend),1) future <- cumsum( c( tail(my.trend,1) , rep(fst,length(vec)-x2) ))[-1] res <- c(my.trend,future) lines(res,t="l",lwd=2) return(res) } 基本上它有效 fu(vect,i[1],i[2]) 只有笨重的结果((
写了这个函数
基本上它有效
只有笨重的结果((