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.

I try to do:


if (my.ds$nest<1000 & my.ds$ClassType!="Class_0_1") { #1)  If `nest` is < 1000 - considering "Class_1_3" to "Class_50"

counts <- my.ds%>%group_by(nest)%>%summarize(N=length(unique(ClassType)))

my.ds.3 <- my.ds%>%group_by(nest)%>%dplyr::summarize(ClassType=ClassType,max_AT=max(AT))
my.ds.3$class_max<- as.numeric(gsub('Class_[^_]*[^_]_', '', my.ds.3$ClassType)) # Max class value - second unscore

if(counts$N>1 | my.ds.3$max_AT>my.ds.3$class_max){ #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 `nest`2 and 7). or ooking for the last `ClassType` before `AT` enter and calculate the `velocity` between the last `ClassType` and the current `ClassType` for nest,

nests <- unique(rbind(counts$nest,my.ds.3$nest))
my.ds.2 <- my.ds[my.ds$nest %in% nests,]

my.ds.2$velocity <- c(0,diff(my.ds.2$AT))/c(0,diff(my.ds.2$days)) # Procurando o Ășltimo ClassType antes de AT e calcula a `velocity` entre o Ășltimo ClassType e o atual ClassType para nest

my.ds.2 <- %>% 
  group_by(nest,ClassType)%>%
  summarize(velocity=mean(velocity)) %>%
  complete(ClassType, tidyr:fill = list(velocity = NA)) %>%
  fill(velocity, .direction = "downup") # 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.

} 

} else { #If nest is >= 1000 - just considering "Class_0_1" `velocity` is `c(0,diff(AT))/c(0,diff(days))` by `days` for each nest too.

my.ds$velocity <- c(0,diff(my.ds$AT))/c(0,diff(my.ds$days)) 

}

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?

You may want to avoid a series of if statements, which is difficult to read, and slow.
Instead, the "switch" command may be helpful.
It is the equivalent to "select case" in other languages.
Good luck!

1 Like

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