我现在有一个代码模拟随机游走的代码,有些算法将它分成特定的间隔,并将数字保存在我称之为#34; liste"目前。这是代码:
x0 <- 0
T <- 100
x <- c(x0,rnorm(T-1))
y <- cumsum(x)
plot(y,type="l")
k<- NULL
d<- NULL
dreisum<- NULL
t <- vector("list",length(x))
A <- vector("list",length(x))
for (i in 1:(length(x)-2)){k <- c(k,print(k <- (y[i]-y[i+2])/(-2)))
d <- c(d,print(d <- (y[i+2]*i-y[i]*(i+2))/(-2)))
dreisum <- c(dreisum,print(dreisum <- sum(x[c((2+(i-1)),(3+(i-1)))])))
t[[i]] <- i:(length(x))
A[[i]] <- k[i]*t[[i]]+d[i]
A[[i]][3] <- y[i+2]}
dreisumi <- vector("list",length(x)-2)
zdreisumi<- vector("list",length(x)-4)
zadreisumi<- vector("list",length(x)-4)
z <- vector("list",length(x)-2)
za <- vector("list",length(x)-2)
yi <- vector("list",length(x)-2)
for(i in 1:(length(x)-2)){dreisumi[[i]] <- dreisum[i:(length(x)-2)]}
for(i in 1:(length(x)-4)){zdreisumi[[i]] <- dreisumi[[i]][3:length(dreisumi[[i]])]<0}
for(i in 1:(length(x)-4)){zadreisumi[[i]] <- dreisumi[[i]][3:length(dreisumi[[i]])]>0}
for(i in 1:(length(x)-2)){yi[[i]] <- y[i:(length(x))]}
for(i in 1:(length(x)-2)){z[[i]] <- yi[[i]][3:length(yi[[i]])]<A[[i]][3:length(A[[i]])]}
for(i in 1:(length(x)-2)){za[[i]] <- yi[[i]][3:length(yi[[i]])]>A[[i]][3:length(A[[i]])]}
i <- 1
h <- 1
liste <- NULL
while(i<=(length(x)-3) & h!=Inf){i <- i+h-1
liste <- c(liste,print(i))
if(k[i]>0 & y[i+3]>y[i+2] & is.element(TRUE,z[[i]])){h <- (min(which(z[[i]]!=FALSE))+1)}else{
if(k[i]>0 & y[i+3]<y[i+2] & is.element(TRUE,za[[i]])){h <- (min(which(za[[i]]!=FALSE))+1)}else{
if(k[i]<0 & y[i+3]>y[i+2] & is.element(TRUE,z[[i]])){h <- (min(which(z[[i]]!=FALSE))+1)}else{
if(k[i]<0 & y[i+3]<y[i+2] & is.element(TRUE,za[[i]])){h <- (min(which(za[[i]]!=FALSE))+1)}else{
if(k[i]>0 & y[i+3]>y[i+2] & (all(z[[i]]==FALSE))){h <- (min(which(zdreisumi[[i]]!=FALSE))+2)}else{
if(k[i]>0 & y[i+3]<y[i+2] & (all(za[[i]]==FALSE))){h <- (min(which(zdreisumi[[i]]!=FALSE))+2)}else{
if(k[i]<0 & y[i+3]>y[i+2] & (all(z[[i]]==FALSE))){h <- (min(which(zadreisumi[[i]]!=FALSE))+2)}else{
if(k[i]<0 & y[i+3]<y[i+2] & (all(za[[i]]==FALSE))){h <- (min(which(zadreisumi[[i]]!=FALSE))+2)}}}}}}}}}
加载代码一次给我一个向量(&#34; liste&#34;)我只想重复n次并将所有向量保存在列表中......有两个不同的警告消息取决于RW看起来像但是结果向量(&#34; liste&#34;)是完全正确的,所以我猜它并不那么重要。我已经尝试过了:
myfunc <- function(n){ .... # whole code above
vectorliste <- vector("list",n)
for(i in 1:n){vectorliste[[i]] <- liste}
return(vectorliste)
}
但它根本不起作用......也许你有一些简单的提示,因为我是一个初学者 谢谢你的回答!