Reputation: 751
My code is given below. The code produces g
values more than once, say g1
, g2
etc. What I want to do is to extract these g values from the loop and use it out of the loop. Any idea how to do so?
I<-1
S<-10
h<-1
lmd<-1
v<-2
n<-100
A=as.integer((S-I)/h)
U=A+1
Sha=(A*h)+I
sindeg=seq(from = I, to =Sha, length.out =U)
D=((lmd)^v)*(((sindeg)^(-v))-((sindeg+h)^(-v)))
tn=(as.integer(n*D))
for(i in 1:A){
print(paste(tn[i]))
atn=tn[i]
k=sindeg[i]+h
m=sindeg[i]
print(paste("alt",m))
print(paste("üst",k))
g=runif(atn, m, k)
print(paste(g))
}
Upvotes: 0
Views: 780
Reputation: 17642
You can use iteration functions such as functions from purrr
instead of raw for loops. Here using map2
to iterate on tn
and sindeg
library(purrr)
gs <- map2( tn, sindeg, function(atn, m) {
runif(atn, m, m + h )
})
Upvotes: 1
Reputation: 744
If i understood your point, you can put the current g values to an external (compared to the loop) list, for example here extg :
I<-1
S<-10
h<-1
lmd<-1
v<-2
n<-100
A=as.integer((S-I)/h)
U=A+1
Sha=(A*h)+I
sindeg=seq(from = I, to =Sha, length.out =U)
D=((lmd)^v)*(((sindeg)^(-v))-((sindeg+h)^(-v)))
tn=(as.integer(n*D))
extg = list()
for(i in 1:A)
{
print(paste(tn[i]))
atn=tn[i]
k=sindeg[i]+h
m=sindeg[i]
print(paste("alt",m))
print(paste("üst",k))
` g=runif(atn, m, k)
extg[[i]] = g
print(paste(g))
}
`
Upvotes: 1