Home > Software engineering >  Particular ratio using dplyr and tidyr
Particular ratio using dplyr and tidyr

Time:07-07

I'd like to create a new velocity variable. In my data set:

library(dplyr)
library(tidyr)

day <- c(0,47,76,118,160,193,227,262,306,355,396,450)
AT <- c(0.14,0.48,0.83,0.83,0.94,0.94,0.94,0.94,0.94,11.93,12.81,29.36)
ClassType <- c("Class_0_1","Class_0_1","Class_0_1","Class_0_1","Class_0_1","Class_0_1",
"Class_0_1","Class_0_1","Class_0_1","Class_9_25","Class_9_25","Class_25_50")
ClassMax <-c(1,1,1,1,1,1,1,1,1,25,25,50)
my.ds <- data.frame(day,AT,ClassType,ClassMax)
my.ds
#    day    AT   ClassType ClassMax
# 1    0  0.14   Class_0_1        1
# 2   47  0.48   Class_0_1        1
# 3   76  0.83   Class_0_1        1
# 4  118  0.83   Class_0_1        1
# 5  160  0.94   Class_0_1        1
# 6  193  0.94   Class_0_1        1
# 7  227  0.94   Class_0_1        1
# 8  262  0.94   Class_0_1        1
# 9  306  0.94   Class_0_1        1
# 10 355 11.93  Class_9_25       25
# 11 396 12.81  Class_9_25       25
# 12 450 29.36 Class_25_50       50

If ClassType changes, take the next AT value minus actual ClassType values and divide by the difference between the two correspondent dates. In my case:

(11.93 0.94) / (355-306)
#[1] 0.2242857
(12.81-11.93) / (396-355)
#[1] 0.02146341
(29.36-12.81) / (450-396)
#[1] 0.3064815

But if AT is in a new ClassType but do not change based in ClassMax then ignore it. I have a min to max custom ordination complte.cases <- c("Class_0_1","Class_1_3","Class_3_9", "Class_9_25","Class_25_50","Class_50"). I'd like to repeat the last velocity value inside the intermediate absent ClassType. I try to do without success:

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

final.ds <- %>% 
  group_by(nest,ClassType)%>%
  summarize(velocity=mean(velocity)) %>%
  complete(ClassType, tidyr:fill = list(velocity = NA)) %>%
  fill(velocity, .direction = "downup") 
} 

My desirable output must to be:

final.ds
#  ClassType   velocity
#  Class_ 0_1  0.224285714
#  Class_ 1_3  0.224285714
#  Class_ 3_9  0.224285714
#  Class_ 9_25 0.224285714
#  Class_ 9_25 0.021463415
#  Class_ 9_25 0.306481481

Please, any help with it?

CodePudding user response:

How about this:

my.ds %>%
  group_by(ClassType) %>%
  mutate(velocity = c(NA, diff(AT) / diff(day))) %>%
  ungroup()
# # A tibble: 12 x 5
#      day    AT ClassType   ClassMax velocity
#    <dbl> <dbl> <chr>          <dbl>    <dbl>
#  1     0  0.14 Class_0_1          1 NA      
#  2    47  0.48 Class_0_1          1  0.00723
#  3    76  0.83 Class_0_1          1  0.0121 
#  4   118  0.83 Class_0_1          1  0      
#  5   160  0.94 Class_0_1          1  0.00262
#  6   193  0.94 Class_0_1          1  0      
#  7   227  0.94 Class_0_1          1  0      
#  8   262  0.94 Class_0_1          1  0      
#  9   306  0.94 Class_0_1          1  0      
# 10   355 11.9  Class_9_25        25 NA      
# 11   396 12.8  Class_9_25        25  0.0215 
# 12   450 29.4  Class_25_50       50 NA      

CodePudding user response:

complete.cases <- c("Class_0_1","Class_1_3","Class_3_9", "Class_9_25","Class_25_50")
my.ds %>% group_by(ClassType = factor(ClassType, levels = complete.cases), grp = lag(match(ClassType, unique(ClassType)), default = 1)) %>% slice_tail(n = 1) %>% 
  ungroup %>%summarise(ClassType, velocity = c(NA, diff(AT))/c(NA, diff(day))) %>% 
  complete(ClassType) %>% 
  fill(velocity, .direction = "updown")
# ClassType   velocity
# <fct>          <dbl>
#   1 Class_0_1     0.224 
# 2 Class_1_3     0.224 
# 3 Class_3_9     0.224 
# 4 Class_9_25    0.224 
# 5 Class_9_25    0.0215
# 6 Class_25_50   0.306 
  • Related