I created a data frame but the loop is not able to detect it.

SimReA<-matrix(data = 0,ncol = 7)
SimReA<-as.data.frame(SimReA)
colnames(SimReA)<-c("SNo", "Batsman", "Bowler","Ball No.", "Probability","Outcome", "Run" )
print(SimReA)
#Inning 1
library("RecordLinkage")
WktCount = 0
OvCOunt = 0
Status = TRUE

i<-1
while((WktCount<nrow(TeamA) && OvCOunt<20) == TRUE){
#Initialising the first inning

#Updation
if(Status == FALSE)
{
break
print("Loop End!")
}else{

if(i == 1)
{
  SimReA[i, "SNo"]<-i
}else{
  
  #Updating Sno
  SimReA[i, "SNo"]<-i
  
  # The number of batsman on the field will be same as the number of wickets down
  #If once a batsman gets out then never returns on the strike in a match
  if(isTRUE(SimReA[i-1,"Outcome"] == "Wkt" | i == 2) == TRUE  )
  {
    SimReA[i,"Batsman"]<-TeamA[WktCount+1, "Player Name"]
    #      WktCount<-WktCount+1
  }else{
    SimReA[i, "Batsman"]<-SimReA[i-1, "Batsman"]
  }
  print(SimReA)
  
  
  
  # Using randomly choosen bowler because same bowler can pitch multiple times in a over
  if (SimReA[i-1, "Ball No."] %% 6 == 0)
  {
    SimReA[i, "Bowler"]<-sample(x = TeamB$`Player Name`, size = 1,replace = TRUE)
  }else
  {
    SimReA[i, "Bowler"]<-SimReA[i-1, "Bowler"]
  }
  print(SimReA)
  
  
  SimReA$`Ball No.`[i]<-SimReA$`Ball No.`[i-1]+1
  
  if(SimReA[i, "Ball No."] %% 6 == 0)
  {
    OvCOunt<-OvCOunt+1
  }else{
    OvCOunt<-OvCOunt
  }
  print(SimReA)
  
  
  
  #Simulation
  SimReA[i,"Probability"]<-runif(n = 1)           
  
  #Getting the bts class and bwl class
  btsclass<-newbts$Class[which.max(levenshteinSim(str1 = SimReA[i, "Batsman"], str2 = newbts$Player))]    
  bwlclass<-newbwl$Class[which.max(levenshteinSim(str1 = SimReA[i, "Bowler"], str2 = newbwl$Player))]     
  
  if(SimReA[i, "Probability"]<=Outcome_0[btsclass, bwlclass])
  {
    SimReA[i, "Outcome"]<-0
  }else if (SimReA[i, "Probability"]>Outcome_0[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_1[btsclass, bwlclass]){
    SimReA[i, "Outcome"]<-1
  }else if (SimReA[i, "Probability"]>Outcome_1[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_2[btsclass, bwlclass]){
    SimReA[i, "Outcome"]<-2
  }else if (SimReA[i, "Probability"]>Outcome_2[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_3[btsclass, bwlclass]){
    SimRe[i, "Outcome"]<-3
  }else if (SimReA[i, "Probability"]>Outcome_3[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_4[btsclass, bwlclass]){
    SimReA[i, "Outcome"]<-4
  }else if (SimReA[i, "Probability"]>Outcome_4[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_6[btsclass, bwlclass]){
    SimReA[i, "Outcome"]<-6
  }else if (SimReA[i, "Probability"]>Outcome_6[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_Wd[btsclass, bwlclass]){
    SimReA[i, "Outcome"]<-"Wd"
  }else if(SimReA[i, "Probability"]>Outcome_Wd[btsclass,bwlclass]  && SimReA[i, "Probability"]<=Outcome_Wkt[btsclass, bwlclass]){
    SimReA[i, "Outcome"]<-"Wkt"
    WktCount<-WktCount+1
  }
  print(SimReA)
  
  
  #Run updation
  if(SimReA[i, "Outcome"]==0)
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+0
  }else if (SimReA[i, "Outcome"] == 1)
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+1
  }else if (SimReA[i, "Outcome"] == 2)
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+2
  }else if (SimReA[i, "Outcome"] == 3)
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+3
  }else if (SimReA[i, "Outcome"] == 4)
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+4
  }else if (SimReA[i, "Outcome"] == 6)
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+6
  }else if (SimReA[i, "Outcome"] == "Wd")
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]+1
  }else if (SimReA[i, "Outcome"] == "Wkt")
  {
    SimReA[i, "Run"]<-SimReA[i-1, "Run"]
  }
  print(SimReA)
}

}

#Status updation
if((WktCount<nrow(TeamA) && OvCOunt<=20) == TRUE)
{
Status <- TRUE
}else{
Status<- FALSE
}
print(SimReA)

i<-i+1
SimReA<-rbind(SimReA, 0)
print(SimReA)
print("")
print(Status)
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
}

This shows the output as:
SNo Batsman Bowler Ball No. Probability Outcome Run
1 1 0 0 0 0.0000000 0 0
2 2 Rahul Dravid Brendon McCullum 1 0.6002869 0 0
3 3 Rahul Dravid Brendon McCullum 2 0.1408549 0 0
4 4 Rahul Dravid Brendon McCullum 3 0.8087207 0 0
5 5 Rahul Dravid Brendon McCullum 4 0.9750249 0 0
6 6 Rahul Dravid Brendon McCullum 5 0.3061971 0 0
7 7 Rahul Dravid Brendon McCullum 6 0.4286289 0 0
8 8 Rahul Dravid Chris Gayle 7 0.5328367 0 0
9 9 Rahul Dravid Chris Gayle 8 0.7416640 0 0
10 10 Rahul Dravid Chris Gayle 9 0.3549797 0 0
11 11 Rahul Dravid Chris Gayle 10 0.6067692 0 0
12 12 Rahul Dravid Chris Gayle 11 0.8602620 0 0
13 13 Rahul Dravid Chris Gayle 12 0.6689219 0 0
14 14 Rahul Dravid Ricky Ponting 13 0.7810466 Wd 1
15 15 Rahul Dravid Ricky Ponting 14 0.0000000 0 0
Error: object 'SimRe' not found

It never escapes the loop. Unlike most procedural languages, such as C and Python variables created within a function are local to that function and are not automatically returned to the global environment. However, in a loop, a return() usually will solely reflect the state of the last iteration.

Here are some patterns

# create a receiver object in the global environment
results <- vector(length = 11)
# loop
for(i in seq_along(mtcars)) results[i] = mean(mtcars[i][[1]])
results
#>  [1]  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250
#>  [7]  17.848750   0.437500   0.406250   3.687500   2.812500

# preferred: built-in function
rowMeans(mtcars)
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>            29.90727            29.98136            23.59818            38.73955 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>            53.66455            35.04909            59.72000            24.63455 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>            27.23364            31.86000            31.78727            46.43091 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>            46.50000            46.35000            66.23273            66.05855 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>            65.97227            19.44091            17.74227            18.81409 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>            24.88864            47.24091            46.00773            58.75273 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>            57.37955            18.92864            24.77909            24.88027 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>            60.97182            34.50818            63.15545            26.26273

# next best: user function vectorized

take_mean <- function(x,y) mean(x[y])
apply(mtcars,2,take_mean)
#>        mpg        cyl       disp         hp       drat         wt       qsec 
#>  20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750 
#>         vs         am       gear       carb 
#>   0.437500   0.406250   3.687500   2.812500

Created on 2023-04-23 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.