I’m trying to find an efficient way to replace a for loop in my code. I’ve found a workaround but I’m sure there is a more “R-friendly” way to do this.

```
df<-structure(list(YY = c(2020, 2020, 2021, 2021, 2021), DD = c(6,
13, 19, 28, 3), MM = c("Fev", "Fev", "Fev", "Fev", "Mar"), Date = structure(c(18664,
18671, 18677, 18686, 18689), class = "Date"), `ID (FIFA)` = c("FRA D1",
"FRA D1", "FRA D1", "FRA D1", "FRA D1"), Country = c("France",
"France", "France", "France", "France"), League = c("Ligue 1",
"Ligue 1", "Ligue 1", "Ligue 1", "Ligue 1"), Season = c("2020/2021",
"2020/2021", "2020/2021", "2020/2021", "2020/2021"), HOME = c("Lyon",
"Lyon", "Brest", "Marseille", "Lyon"), AWAY = c("Strasbourg",
"Montpellier", "Lyon", "Lyon", "Rennes"), `Final Scores` = c(3,
1, 2, 1, 1), ...12 = c(0, 2, 3, 1, 0), ...13 = c("H", "A", "A",
"D", "H"), ...14 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), `ET/Pen/Awd` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_),
`1st Half Scores` = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), ...17 = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), ...18 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), ...19 = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), `2nd Half Scores` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), ...21 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), ...22 = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), ...23 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), FTMoneyline...24 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), ...25 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), ...26 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), `Payout, %...27` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), FTMoneyline...28 = c(1.45,
1.38, 6.5, 5, 1.61), ...29 = c(5.25, 5.5, 4.85, 4.2, 4.4),
...30 = c(8, 8, 1.5, 1.7, 5.9), `Payout, %...31` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), `FT TG 2.5...32` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), ...33 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), `FT TG 2.5...34` = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), ...35 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), OUTCOME = c(1, 3,
3, 2, 1), REFGAME = c("G236038", "G236098", "G236155", "G236247",
"G236276"), m1 = c(4.9, 4.96, 4.28333333333333, 3.63333333333333,
3.97), sd = c(3.28899680753874, 3.34287301583533, 2.54771139129481,
1.72143351115671, 2.17708520733572), indxcol = c(1L, 1L,
3L, 3L, 1L), sp1 = c(-3.8, -4.12, 1.65, 0.8, -2.79), sp2 = c(2.75,
2.5, -3.35, -2.5, 1.5), ovr = c(0.513136288998362, 3.14558629776023,
2.66983875231297, 2.63305322128851, 1.78822651188164), wavg = c(0.357142857142857,
0.369623655913978, 0.377431906614786, 0.385321100917431,
0.36943744752309), ospr = c(5.51724137931035, 5.79710144927536,
4.33333333333333, 2.94117647058824, 3.66459627329193)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
```

The objective of my script is to count for each HOME team how many times in the previous games it was the bookmaker’s lowest decimal odds (odds are in col df(28:30)), and how many times it actually corresponded to the OUTCOME of the game.

This way I can get an accuracy ratio (r/t*100).

Here is the code with the for loop part I want to replace:

```
if(is.null(nrow(df))|nrow(df)==0){
r<-0
return(r)
}
r<- 0
t<- 0
for(i in nrow(df):1){
if(grep(team,df(i,))==9 & which.min(df(i,28:30))((1))==1){
t<-t+1
if(df(i,"OUTCOME")==1){
r<-r+1
}
} else if(grep(team,df(i,))==10 & which.min(df(i,28:30))((1))==3){
t<-t+1
if(df(i,"OUTCOME")==3){
r<-r+1
}
}
}
if(r==0 | t==0){
return(0)
}
return(r/t*100)
```

And here is the workaround I’ve found but it doesn’t look optimal:

```
r<- 0
t<- 0
df1<- df(grepl(team,df$HOME) & apply(df(28:30),1,which.min)==1,)
if(is.null(nrow(df1))|nrow(df1)==0){
r<-0
return(r)
}
r1 <-df1(df1("OUTCOME")==1,)
df2<- df(grepl(team,df$AWAY) & apply(df(28:30),1,which.min)==3,)
if(is.null(nrow(df2))|nrow(df2)==0){
r<-0
return(r)
}
r2<- df2(df2("OUTCOME")==3,)
t<- sum(nrow(df1),nrow(df2))
r<-sum(nrow(r1),nrow(r2))
if(r==0 | t==0){
return(0)
}
return(r/t*100)
```