Hi all,
I have a problem with the script below. My goal is to apply the first function to the first 10 rows of my database and the second function to the second set of rows (from 11 to 309 rows). I would like the second function to be applied recursively, i.e. when my counter reaches line 310 it is reset to 11 and the second function restarts from the beginning. At the moment it seems that the problem is in the last function.
Thanks
library(matrixStats)
library(tidyverse)
library(glue)
library(dplyr)
#database
db_class <- economics_long
Condition <- db_class[,2]
#first parameter and first limit
firstParameter <- (db_class[1,3])
Limits <- firstParameter*1.5
#counter
(db_class2 <-db_class %>%
mutate(rgroup = row_number() <= 309) %>%
group_by(rgroup) %>%
mutate( rn=row_number(),
rn2 = ifelse(rgroup,(rn -1) %% 309 + 1,
(rn-1) %% 291 + 10)
) %>% ungroup)
#first function
firstControll <- function(database){
upper <- Limits
column <- ncol(database)+1
for (i in 1:10) {
if (database[i, 3] < upper) {
Cont <- "OK"
db_class[i, column] <<- Cont
} else {
Cont <- "ERROR"
db_class[i, column] <<- Cont
}
}
}
#second function
secondControll <- function(database){
column <- ncol(database)+1
average <- mean(db_class[11:300,3])
Limits <- 0.2*firstParameter + (1-0.2)*average*1.5
upper <<- Limits
for (i in 11:300) {
if (database[i, 3] < upper) {
Cont <- "OK"
db_class[i, column] <<- Cont
} else {
Cont <- "ERROR"
db_class[i, column] <<- Cont
}
}
}
#total function
TotalControl <- function(database){
for(i in database[,ncol(database)]){
if (database[,ncol(database)] < 11) {
firstControll(database)
}else{
if (database[,ncol(database)] > 10 & database[,ncol(database)] < 301){
secondControll(database)
}
}
}
}
TotalControl(db_class2)