Conditional function

In my case:


# Open dataset

library(dplyr)

my.ds <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/test_ants.csv")

my.ds

# days nest       AT

# 1     0    2  10.9200

# 2    47    2  22.8600

# 3    76    2  23.2400

# 4     0    3   0.1400

# 5    47    3   0.4800

# 6    76    3   0.8300

# 7   118    3   0.8300

# 8   160    3   0.9400

# 9   193    3   0.9400

# 10  227    3   0.9400

# 11  262    3   0.9400

# 12  306    3   0.9400

# 13  355    3  11.9300

# 14  396    3  12.8100

# 15  450    3  29.3600

# 16    0    4   1.0000

# 17   76    4   1.5600

# 18  160    4   2.8800

# 19  193    4   2.8800

# 20  227    4   2.8800

# 21  262    4   2.8800

# 22  306    4   2.8800

# 23  355    4  17.0500

# 24    0    7  11.7100

# 25   47    7  24.7900

# 26   76    7  33.1200

# 27    0   10   4.1400

# 28   47   10   6.3000

# 29   76   10  13.9400

# 30  118   10  13.3000

# 31  160   10  29.4200

# 32  193   10  29.6000

# 33  227   10  45.1500

# 34  262   10  45.1500

# 35  306   10  45.1500

# 36  355   10  70.5300

# 37  396   10 109.2600

# 38  450   10 149.2000

# 39    0 1066   0.0289

# 40   29 1066   0.0870

# 41   70 1066   0.1254

# 42  112 1066   0.1254

# 43  146 1066   0.1254

# 44  180 1066   0.1254

# 45  215 1066   0.1350

# 46  259 1066   0.1350

# 47    0 1067   0.0520

# 48   29 1067   0.1254

# 49   70 1067   0.1440

# 50  112 1067   0.2160

# 51  146 1067   0.2538

# 52  180 1067   0.2538

# 53  215 1067   0.9600

# 54  259 1067   0.9600

# 55  349 1067   0.9600

# Class size

my.ds$ClassType <- cut(my.ds$AT,breaks=c(-Inf,1,2.9,8.9,24.9,49.9,Inf),right=FALSE,

                          labels=c("Class_0_1","Class_1_3","Class_3_9","Class_9_25","Class_25_50","Class_50"))

I'd to calculate a ratio variable velocity with the following rules:

  1. If nest is < 1000 - considering "Class_1_3" to "Class_50"

1.1) If AT is < 50

1.1.1) If ClassType do not change by nest during the time or AT enter but dont leave the ClassType, don't calculate velocity (In my case nest2 and 7).,

1.1.2) Looking for the last ClassType before AT enter and calculate the velocity between the last ClassType and the current ClassType for nest

1.1.3) If lag any ClassType in "Class_1_3" to "Class_50" in a class more than AT value in the time 0, the class not present is the velocity value of the last ClassType for nest.

1.2) If AT is >= 50

1.2.1) velocity is c(0,diff(AT))/c(0,diff(days)) by days for each nest

  1. If nest is >= 1000 - just considering "Class_0_1"

2.1) velocity is c(0,diff(AT))/c(0,diff(days)) by days for each nest too.

My desirable output is:


# nest  ClassType   velocity

# 3 Class_ 1_3  0.224285714

# 3 Class_ 3_9  0.224285714

# 3 Class_ 9_25 0.224285714

# 3 Class_ 9_25 0.021463415

# 3 Class_ 9_25 0.306481481

# 4 Class_ 1_3  0.0075

# 4 Class_ 1_3  0.015714286

# 4 Class_ 1_3  0

# 4 Class_ 1_3  0

# 4 Class_ 1_3  0

# 4 Class_ 1_3  0

# 4 Class_ 1_3  0.289183673

# 4 Class_ 3_9  0.289183673

# 10    Class_9_25  0.263448276

# 10    Class_9_25  -0.015238095

# 10    Class_9_25  0.383809524

# 10    Class_25_50 0.383809524

# 10    Class_25_50 0.005454545

# 10    Class_25_50 0.457352941

# 10    Class_25_50 0

# 10    Class_25_50 0

# 10    Class_25_50 0.517959184

# 10    Class_50    0.517959184

# 10    Class_50    0.944634146

# 10    Class_50    0.73962963

# 1066  Class_0_1   0.002003448

# 1066  Class_0_1   0.000936585

# 1066  Class_0_1   0

# 1066  Class_0_1   0

# 1066  Class_0_1   0

# 1066  Class_0_1   0.000274286

# 1066  Class_0_1   0

# 1067  Class_0_1   0.002531034

# 1067  Class_0_1   0.000453659

# 1067  Class_0_1   0.001714286

# 1067  Class_0_1   0.001111765

# 1067  Class_0_1   0

# 1067  Class_0_1   0.020177143

# 1067  Class_0_1   0

# 1067  Class_0_1   0

Please any help with it?