我正在处理演讲记录:
Utterance Starttime_ms Endtime_ms
<chr> <dbl> <dbl>
1 on this 210 780
2 okay 3403 3728
3 cool thanks everyone um 4221 5880
4 so yes in terms of our projects 5910 11960
5 let's have a look so the 11980 13740
6 LGBTQ plus 13813 16110
并希望在每个Utterance
之后插入一个新行,指示与前Utterance
的时间间隔.desired output看起来有点像这样:
Utterance Starttime_ms Endtime_ms
<chr> <dbl> <dbl>
1 on this 210 780
NA 780 3403
2 okay 3403 3728
NA 3728 4221
3 cool thanks everyone um 4221 5880
NA 5880 5910
4 so yes in terms of our projects 5910 11960
NA 11960 11980
5 let's have a look so the 11980 13740
NA 13740 13813
6 LGBTQ plus 13813 16110
我知道如何在data.table
内做到这一点:
library(data.table)
unq <- c(0, sort(unique(setDT(df)[, c(Starttime_ms, Endtime_ms)])))
df <- df[.(unq[-length(unq)], unq[-1]), on=c("Starttime_ms", "Endtime_ms")]
但我正在寻找dplyr
个解决方案.
生效日期:
df <- structure(list(Utterance = c("on this", "okay", "cool thanks everyone um",
"so yes in terms of our projects",
"let's have a look so the", "LGBTQ plus"), Starttime_ms = c(210,
3403, 4221, 5910, 11980, 13813), Endtime_ms = c(780, 3728, 5880,
11960, 13740, 16110)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))