So, i have this small database that i'm working with in RStudio, and I have this situation where i need to classify some students based on which classes they flunked.
for (i in 1:(nrow(BDbruno2)-1))
{
#avoiding exploitations, i tested without it but my problem still continues
if(i >= nrow(BDbruno2))
{
break
}else
{
#checking is the codes are still the same
if((BDbruno2$Code[i] == BDbruno2$Code[i+1]) && (i < nrow(BDbruno2)))
{
auxIndice <- BDbruno2$nLinha[i]
auxTurmas <- BDbruno2$tempo[i]
for(j in (i+1):nrow(BDbruno2))
{
#checking if codes are the same, if FALSE, i save all classes and save in a string for all codes
if(BDbruno2$Code[j-1] != BDbruno2$Code[j]){
BDbruno2$turmasCalc1[auxIndice] <- paste0(auxTurmas, collapse = " ")
#skipping the same codes that i already checked
i <- j
#i tested without this break, only makes my code to take longer to finish
break
} else
{
#saving all rows where the code is the same
auxIndice <- c(auxIndice, BDbruno2$nLinha[j])
#this line below is where i get my problem:
#it receives the classes from the same code, but when going further in the loop, this var gets messed
auxTurmas <- c(auxTurmas, BDbruno2$tempo[j])
}
}
}
}
}
The auxTurmas
var won't return the expected result, but it does when i run the code line-by-line.
There are these 4 cases where i get the same student (different rows), i save all his classes in a new var (turmasCalc1
), which are 1 4 7 8
, and all his rows gets theses numbers, but when inside a for-loop, the first one is correct, the second fails (4 7 8
), the other two also fail(7 8
).
Weirdly, if i run it from i = nLinha in 1:46
, it works as it should, but i need it for all my cases (which happens a lot). I'm not sure, but it doesn't seem to be a issue with the break
i used, but i can't see what makes this strange thing happen. Can somebody give me a light?
Edit: Sorry for the lack of information, here is a sample of the data frame, it's supposed to return 1 4 7 8
on row nLinha = 46:49
, similar problem occurs other times on same data frame.
Code Calculo1_Turma tempo turmasCalc1 nLinha
1635340632 2014/1 - MAT154-B 11 11 45
1638717605 2009/1 - MAT154-E 1 1 4 7 8 46
1638717605 2010/3 - MAT154-I 4 4 7 8 47
1638717605 2012/1 - MAT154-A 7 7 8 48
1638717605 2012/3 - MAT154-D 8 7 8 49
1643222643 2011/1 - MAT154-D 5 5 6 50
1643222643 2011/3 - MAT154-B 6 5 6 51
1645485641 2009/1 - MAT154-B 1 1 52
This is what I'm trying to get:
Code Calculo1_Turma tempo turmasCalc1 nLinha
1635340632 2014/1 - MAT154-B 11 11 45
1638717605 2009/1 - MAT154-E 1 1 4 7 8 46
1638717605 2010/3 - MAT154-I 4 1 4 7 8 47
1638717605 2012/1 - MAT154-A 7 1 4 7 8 48
1638717605 2012/3 - MAT154-D 8 1 4 7 8 49
1643222643 2011/1 - MAT154-D 5 5 6 50
1643222643 2011/3 - MAT154-B 6 5 6 51
1645485641 2009/1 - MAT154-B 1 1 52
Edit2: Sorry again for the confusion. Here is the dpyr-generated code:
BDbruno2 <- structure(list(Code = c("1634171640", "1634171640", "1634171640", "1635340632", "1638717605", "1638717605", "1638717605", "1638717605", "1643222643", "1643222643", "1645485641"), Calculo1_Turma = c("2009/1 - MAT154-D", "2009/3 - MAT154-A", "2010/3 - MAT154-I", "2014/1 - MAT154-B", "2009/1 - MAT154-E", "2010/3 - MAT154-I", "2012/1 - MAT154-A", "2012/3 - MAT154-D", "2011/1 - MAT154-D", "2011/3 - MAT154-B", "2009/1 - MAT154-B"), tempo = c(1, 2, 4, 11, 1, 4, 7, 8, 5, 6, 1), turmasCalc1 = c("1", "2", "4", "11", "1 4 7 8", "4 7 8", "7 8", "7 8", "5 6", "5 6", "1"), nLinha = 42:52), .Names = c("Code", "Calculo1_Turma", "tempo", "turmasCalc1", "nLinha"), row.names = c(162L, 305L, 714L, 3880L, 210L, 715L, 887L, 924L, 2157L, 2446L, 60L), class = "data.frame")
This one generates a few more row below, which are working as they should. Just a recap: i'm supposed to get 1 4 7 8
on turmasCalc1
where nLinha in 46:49
, but there seems to be a issue with the i index. This issue happens when the same code appears 3 or more times, not only specifically 4.