我正在尝试使用end_time
和R
语句,根据dplyr::mutate()
中已知间隔的ifelse()
定义间隔的开始。
我可以使用最小值时间值轻松地为第一个间隔定义start_time
,但会被其他开始时间所困扰。我尝试使用dense_rank()
对它们进行排名,但是我不知道为先前的end_time
值提取ranked
的正确语法。 start_time
的{{1}}应该等于先前ranked > 1
值的end_time + 1
。
ranked
期望的结果是:
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))
答案 0 :(得分:0)
我们可以尝试将dplyr::lag
与deafult=-1
并添加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