Counter function

Hi!
I have a problem with a Counter function. I want to set a column in my database with a counter that when it arrive to 500, it restart to 10. This is my actual code:

contt <- function(){
Counter <- c(1:nrow(db_class))
Cont <- c(1:501)
Cont2 <- c(10:500)
if (Counter < 500){
  db_class$Counter = Cont
}
}
contt()

But, actually, it doesn't work.

Thank you!

library(tidyverse)

#example_data
db_class <- economics_long
nrow(db_class)

#a solution without loops, but with modulo arithmetic
(db_class2 <-db_class %>% 
  mutate(rn=(row_number() -1) %% 500 + 1))

#examining the result
slice(db_class2,498:502)

Great it works, but if I want to restart from 10?

#a solution without loops, but with modulo arithmetic
(db_class2 <-db_class %>% 
    mutate(rgroup = row_number() <= 500) %>%
    group_by(rgroup) %>%
    mutate( rn=row_number(),
            rn2 = ifelse(rgroup,(rn -1) %% 500 + 1,
                             (rn-1) %% 491 + 10)
           ) %>% ungroup) 

#examining the result
slice(db_class2,498:510)
#examining the result
slice(db_class2,988:1000)

what sort of use case do you have ? :smiley:

Thank you it works :partying_face:

I use this type of counter because I want to run a function on the first 10 rows, and a second function on a range a second function repeated on a scalar range of 500 rows

(Can you explain to me what is the meaning of %>% and where I can use it?)

that implies you dont care about more than 510 rows ? in which case my solution is overkill and you should go with a simpler solution

I have a database with about 1.300 rows and what I want is:
1:10 rows -> function a
11:500 rows -> function b
501:1000 rows -> restart function b
and so on, until the end of the lines

but this implies that function b first gets 490 rows to process and then gets 500 rows to process.
which is different than my latest solution... is that correct or not ?

Mmmh yes, for this reason I want to reset the counter from 10 to to 500 and not from 1

surely it should be setting length(10:509) = 500

assuming so...

db_class <- economics_long
nrow(db_class)

library(glue)
library(tidyverse)
#a solution without loops, but with modulo arithmetic
(db_class2 <-db_class %>% 
   mutate(rn=row_number(),
          g = case_when(rn<=10 ~ "func a",
                        TRUE  ~ paste0("func b " ,1 + (rn %/% 500)))))

table(db_class2$g)

Instead of "func a" and "func b", should I write the name of my function?

but with setting lenght (10:509) I have anyway the range lenght:
first 10
second 289
third 300 and so on

its just an example that I can categorise consecutive rows in the desired way.
You haven't attemped to give an example of a function you have in mind, or how you intend to call it ...

I dont know what you are telling me with 289 and 300 and whatever, ...

In the latest version of my code, the final function was:

TotalControl <- function(database){
  for(i in 1:nrow(database)){
    if (i < 11) {
      FirstControll(database)
    }else{
      if (i > 10 & i < 501){
        SecondControll(database)
      }
      }
    }
  }
}

TotalControl(db_class)

Now I want that when my algorithm arrives at line 501, it reads it as line 11 and repeats the instructions of the SecondControll function

I urge you to think carefully about expressing your explanations.
now your code and explanations all point to treating the first 10 records one way, and all the rest of the records another way. this means again that we have wasted time on complicated solutions for simple outcomes :smiley:

Here is a simple example

library(tidyverse)

func_1_make_neg <- function(x){-x}
func_2_square <- function(x){x*x}

db_class <- economics_long
nrow(db_class)

library(glue)
library(tidyverse)

(db_class2 <-db_class %>% 
    mutate(rn=row_number(),
           result = ifelse(rn<=10 ,
                           func_1_make_neg(value),
                           func_2_square(value))))

head(db_class2)
tail(db_class2)

I have this error:

> Error: Problem with `mutate()` input `result`.
> x new columns would leave holes after existing columns
> ℹ Input `result` is `ifelse(rn <= 10, FirstControll(db_class2), SecondControll(db_class2))`.
> Run `rlang::last_error()` to see where the error occurred.

I guess you've written code which mistakenly tries to pass dataframes to functions called within a mutate.

I recommend you invest a short amount of time and effort and make a reprex (as I have been doing)

library(matrixStats)
library(tidyverse)
library(glue)
library(dplyr)

db_class <- economics_long
Condition <- db_class[,2]

firstParameter <- (db_class[1,3])
Limits <- firstParameter*1.18

(db_class2 <-db_class %>% 
    mutate(rgroup = row_number() <= 509) %>%
    group_by(rgroup) %>%
    mutate( rn=row_number(),
            rn2 = ifelse(rgroup,(rn -1) %% 509 + 1,
                         (rn-1) %% 491 + 10)
    ) %>% ungroup) 

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
    }
  }
}

secondControll <- function(database){
  column <- ncol(database)+1
  
  average <- mean(db_class[11:500,3])
  Limits <- 0.4*firstParameter + (1-0.4)*average*1.18
  upper <<- Limits
  
  for (i in 11:500) {
    if (database[i, 3] < upper) {
      Cont <- "OK"
      db_class[i, column] <<- Cont
    } else {
      Cont <- "ERROR"
      db_class[i, column] <<- Cont
    }
  }
}

(db_class2 <-db_class %>% 
    mutate(rn=row_number(),
           result = ifelse(rn<=10 ,
                           firstControll(db_class2),
                           secondControll(db_class2))))

head(db_class2)
tail(db_class2)

Ok, I make a replex of my code (sorry I'm a beginner and I didn't know how to make it). So the functions work, but I can't get them to start for the various ranges

This topic was automatically closed 7 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.