Foro de debate

por que no me guarda valores, me sale null y na?? alguna idea , que hago mal aqui .

por que no me guarda valores, me sale null y na?? alguna idea , que hago mal aqui .

de Malvis Valenzuela -
Número de respuestas: 2

set.seed(12345)
simulation <- function(TC,QOT,WT,A,CV,TOTruck){
#Initialization
t_collected <- 0
t_notcollected <- 0
t_recolection <- 0
tgoinglandfill<- 0
treturningblock<- 0
totalWT<- WT[1]
WremainigT<-0
AcumulatedWT<-0
AcumulatedTrashTones<-0

TotalCT <- TC[1]*(QOT)
TontrashB <- rnorm(A,CV[2],CV[1])
AvailableCT <- TotalCT -t_collected

i=1
for (i in 1:length(TontrashB[i+1])){
if(AvailableCT[i] > 0 ){
if(TontrashB[i]<= AvailableCT[i]){
if(WremainigT[i]< totalWT[i])
t_collected[i+1]<-TontrashB[i]
t_notcollected[i+1]<- 0
AvailableCT[i+1]<- AvailableCT[i+1]-t_collected[i+1]
AcumulatedTrashTones[i]<-cumsum(t_notcollected[i])
#Tiempo
t_recolection [i] <- rnorm( 1,TOTruck[1],TOTruck[2])
}else {
t_collected[i+1] <- AvailableCT[i]
t_notcollected[i+1]<- TontrashB [i]-AvailableCT[i]
AvailableCT[i+1]<- AvailableCT[i]-t_collected[i]
#Result[i,4]<- ton_garbage[i]-Result[i,2]
AcumulatedTrashTones[i]<-cumsum(t_notcollected[i])
#Result[i,6]<-sum(c_truck)
t_recolection [i+1] <- rnorm( 1,TOTruck[1],TOTruck[2])
tgoinglandfill[i+1] <- rnorm( 1,TOTruck[1],TOTruck[2])
treturningblock[i+1] <- rnorm( 1,TOTruck[1],TOTruck[2])

}
#t_journey[i] <- rnorm(n = 1,1,0.5)
#tiempo<-(t_recolection[i])
AcumulatedWT[i]<-sum(c(tgoinglandfill,treturningblock,t_recolection ))
WremainigT[i]<-totalWT[i] -AcumulatedWT[i]
AcumulatedTrashTones[i]<-AcumulatedTrashTones[i+1]


#tiemp<-cumsum(c(t_recolection,t_journey))
#Result[i,7]<-tiemp[3]
#Result[i,8]<-Result[i,7]+tiemp[3]
}
AvailableCT [i+1]<-AvailableCT [i]

t_recolection[i]<-is.na(t_recolection) + is.na(t_recolection[i])#[3]
AcumulatedWT[i]<-sum(c(tgoinglandfill,treturningblock,t_recolection))

list(t_collected=round(t_collected,2),
t_notcollected=round(t_notcollected,2))
}
}
x <- replicate(10,{
y <- simulation( TC=10,
QOT=1,
WT=8,
A=24,
CV=c(1,4),
TOTruck=c(4,2))

c(y$t_collected,y$t_notcollected,y$totalWT,
y$t_recolection,y$treturninglandfill)
})

 

x1 <- data.frame(t(x))
colnames(x1) <- c("T_collected","t_notcollected","Totalworkingtime",
"recollectiontime")

 

T_collected t_notcollected Totalworkingtime
1 NULL NULL NULL
recollectiontime NA NA NA NA NA NA
1 NULL NULL NULL NULL NULL NULL NULL
>

En respuesta a Malvis Valenzuela

Re: por que no me guarda valores, me sale null y na?? alguna idea , que hago mal aqui .

de Manuel Muñoz Márquez -
Buenas,
Tu código es muy largo y tiene muy pocos comentarios para que se puede entender, intentaré echarle un vistazo más despacio.
A primera vista, no veo donde estás definiendo T_collected, para mí T_collected es una columna de x1, ¿no? y, por tanto, debes acceder a ella como x1$T_collected

¿Has probado la función simulation por separado? Asegúrate de que funciona antes de integrarla dentro de otro trozo de código más grande.

Puedes añadir algunos print para comprobar los resultados intermedios.

Un saludo.