简体   繁体   中英

Conditional mutate statement based on ranked order in R using dplyr::mutate() and ifelse()

I am trying to define the start of an interval based on the known end_time of that interval in R using dplyr::mutate() with an ifelse() statement.

I can define the start_time for the first interval easily using minimum value time value but am getting stuck with the other start times. I've tried ranking them using dense_rank() , but I do not know the proper syntax to extract the end_time for the previous ranked value. The start_time for ranked > 1 should equal the end_time + 1 for the previous ranked value.

library(dplyr)

blks <- data.frame(Group = c(rep("A", 3), rep("B", 4)),
                   end_time = c(4, 8, 20, 5, 11, 15, 20))

expand.grid(time = 0:20,
            Group = c("A","B")) %>% 
   left_join(mutate(blks, time = end_time), by = c("Group", "time")) %>% 
   group_by(Group) %>% 
   mutate(ranked = dense_rank(end_time),
          start_time = ifelse(ranked == 1, min(time), "WHERE I NEED HELP"))
                         # else = the end_time from the previous ranked + 1
                         # end_time[ranked == ranked-1] + 1))

Desired result is:

mutate(blks, start_time = c(0, 5, 9, 0, 6, 12, 16))  

We can try dplyr::lag with deafult=-1 then add 1

library(dplyr)
blks %>% group_by(Group) %>% mutate(start_time = lag(end_time,default=-1)+1)

# A tibble: 7 x 3
# Groups:   Group [2]
    Group end_time start_time
   < fct>    <dbl>      <dbl>
  1 A            4          0
  2 A            8          5
  3 A           20          9
  4 B            5          0
  5 B           11          6
  6 B           15         12
  7 B           20         16 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM