Using a nesting of for-loops and if statements, I try to sort a larger list by iterations of smaller chunks.
Looping through 100 items in total each time comparing items w/in one group like so: 1-5, 6-10, 11-15, etc. groups
Also, if comparing last item of group and first one in the next, e.g. # 5 and 6, don't compare the next one and just do something else.
I want to have different values added to extra columns of a dataframe (excluded from code below to keep it short, focus is on the control flow) based on comparing the value of the current row with the next one.
The input: (Note: colx serves merely to illustrate a dataframe, not relevant)
ColX Somecol
[1] x 0
[2] x 0
[3] x 1
.
.
.
[18] x 1
[19] x 1
[20] x 1
This is the desired output: detecting and categorizing movement.
Somecol Category Changelocation Uppermove Relevant
[1] 0 1 0 1 1
[2] 0 1 0 1 1
[3] 1 1 1 1 1
[4] 1 1 0 1 1
[5] 1 1 0 1 1
[6] 1 2 0 0 0
[7] 1 2 0 0 0
[8] 0 2 1 0 0
[9] 0 2 0 0 0
[10] 0 2 0 0 0
[11] 0 3 0 0 0
[12] 0 3 0 0 0
[13] 0 3 0 0 0
[14] 0 3 0 0 0
[15] 0 3 0 0 0
[16] 1 4 0 0 1
[17] 1 4 0 0 1
[18] 1 4 0 0 1
[19] 1 4 0 0 1
[20] 1 4 0 0 1
[21] 0 5 0 0 1
[22] 0 5 0 0 1
[23] 1 5 1 0 1
[24] 1 5 0 0 1
[25] 0 5 1 0 1
[26] 1 6 0 1 1
[27] 1 6 0 1 1
[28] 0 6 1 1 1
[29] 0 6 0 1 1
[30] 1 6 1 1 1
- Category (1 if upward, 2 if downward, 3 if stayed 0, 4 if stayed 1, 5 if 0-1-1, 6 if 1-0-1)
- Other columns with extra information like location of change (if any), uppermove iff (Direction == 1), and
- Relevant iff ((Category == 1) or (Category == 4) or (&& Category == 5), (Category == 6)).
This is the code so far, any tips appreciated:
funn <- function(abc) {
outercounter <- 0
innercounter <- 0
checker <- function(ic){
if (ic < 5) {
innercounter <- innercounter + 1
outercounter <- outercounter + 1
}
if (ic == 4) {
innercounter <- 2
outercounter <- outercounter + 1
}
}
checker_end <- function(ic2) {
innercounter <- 1
outercounter <- outercounter + 1
}
}
for (i in 1:20) {
for (j in 1:5) {
if ((i == 1) && (innercounter == 0) && (outercounter == 0)) {
outercounter <- outercounter + 1
innercounter <- innercounter + 1
}
if ((innercounter == 1) && (dataframe$column[outercounter] == 1)) {
# do somethingA
checker(innercounter)
}
if ((innercounter == 1) && (dataframe$column[outercounter] == 0)) {
# do somethingB
checker(innercounter)
}
if ((innercounter > 1) && (innercounter < 5) && (dataframe$column[outercounter] < dataframe$column[outercounter+1])) {
# do somethingC
checker(innercounter)
}
if ((innercounter > 1) && (innercounter < 5) && (dataframe$column[outercounter] == 1) && (dataframe$column[outercounter+1] == 1)) {
# do somethingD
checker(innercounter)
}
if ((innercounter > 1) && (innercounter < 5) && (dataframe$column[outercounter] == 1) && (dataframe$column[outercounter+1] == 0)) {
# do somethingE
checker(innercounter)
}
if ((innercounter == 5) && (dataframe$column[outercounter] == 0)) {
# do somethingF
checker_end(innercounter)
}
if ((innercounter == 5) && (dataframe$column[outercounter] == 1)) {
# do somethingG
checker_end(innercounter)
}
}
}
}
fun(10)
Aucun commentaire:
Enregistrer un commentaire