我正在处理演讲记录:

  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"))

推荐答案

您可以try 下面的代码

df %>%
   pivot_longer(-Utterance, values_to = "Starttime_ms") %>%
   mutate(Endtime_ms = lead(Starttime_ms)) %>%
   drop_na() %>%
   select(-name) %>%
   mutate(Utterance = replace(Utterance, !row_number() %% 2, NA_character_))

这给

# A tibble: 11 × 3
   Utterance                       Starttime_ms Endtime_ms
   <chr>                                  <dbl>      <dbl>
 1 on this                                  210        780
 2 NA                                       780       3403
 3 okay                                    3403       3728
 4 NA                                      3728       4221
 5 cool thanks everyone um                 4221       5880
 6 NA                                      5880       5910
 7 so yes in terms of our projects         5910      11960
 8 NA                                     11960      11980
 9 let's have a look so the               11980      13740
10 NA                                     13740      13813
11 LGBTQ plus                             13813      16110

R相关问答推荐

逐行替代引用前一行的for循环

过滤Expand.Grid的结果

将带有范围的字符串转换为R中的数字载体

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

是否可以 Select 安装不带文档的R包以更有效地存储?

如何替换R中数据集列中的各种字符串

使用R的序列覆盖

selectInput不返回ALL,并将因子转换为shiny 的数字

如何使用按钮切换轨迹?

从R导出全局环境中的所有sf(numrames)对象

为什么在ggplot2中添加geom_text这么慢?

我想在R中总结一个巨大的数据框架,使我只需要唯一的lat、lon、Date(Year)和Maxium Value""""""""

如何在格子中添加双曲曲线

汇总数据表中两个特定列条目的值

从一个列表的框架中移除列表包装器

SHINY:使用JS函数应用的CSS样式显示HTML表格

根据约束随机填充向量的元素

循环遍历多个变量,并将每个变量插入函数R

是否有可能从边界中找到一个点值?

自定义交互作用图的标签