创建第a行上的end_time变量,从start_time变量在行B上创建

发布于 2025-02-11 18:40:06 字数 991 浏览 0 评论 0原文

我想根据每个参与者的start_times和time_end_of_experiment(减去10毫秒)为每个参与者创建一个end_time变量,但完全不确定该怎么做。

这是一个最小的工作示例

df <- data.frame("subject_nr" = c("1", "1", "1", "2", "2"),
                 "start_time" = c(50, 52, 55, 53, 54.5),
                 "time_end_of_experiment" = c(60, 60, 60, 55.5, 55.5))


  subject_nr start_time time_end_of_experiment
1          1       50.0                   60.0
2          1       52.0                   60.0
3          1       55.0                   60.0
4          2       53.0                   55.5
5          2       54.5                   55.5

,这是最终产品的外观

  subject_nr start_time end_time time_end_of_experiment  
1          1       50.0     51.9                     60
2          1       52.0     54.9                     60
3          1       55.0     59.9                     60
4          2       53.0     54.4                   55.5
5          2       54.5     55.4                   55.5

I would like to create an end_time variable for each participant based on their start_times and time_end_of_experiment (minus say 10 ms), but quite unsure how to do this.

Here's a minimal working example

df <- data.frame("subject_nr" = c("1", "1", "1", "2", "2"),
                 "start_time" = c(50, 52, 55, 53, 54.5),
                 "time_end_of_experiment" = c(60, 60, 60, 55.5, 55.5))


  subject_nr start_time time_end_of_experiment
1          1       50.0                   60.0
2          1       52.0                   60.0
3          1       55.0                   60.0
4          2       53.0                   55.5
5          2       54.5                   55.5

Here's what the final product should look like

  subject_nr start_time end_time time_end_of_experiment  
1          1       50.0     51.9                     60
2          1       52.0     54.9                     60
3          1       55.0     59.9                     60
4          2       53.0     54.4                   55.5
5          2       54.5     55.4                   55.5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2025-02-18 18:40:06

这是我们可以做到的:
首次使用LEADLead(start_time)提取0.1
然后,对于组中的最后一个值,请使用ifelse语句从time_end_of_experiment提取0.1:

library(dplyr)

df %>% 
  group_by(subject_nr) %>% 
  mutate(end_time = lead(start_time, default = last(start_time))-.1, .before=3) %>% 
  mutate(end_time = ifelse(start_time == last(start_time), time_end_of_experiment-.1, end_time))
  subject_nr start_time end_time time_end_of_experiment
  <chr>           <dbl>    <dbl>                  <dbl>
1 1                50       51.9                   60  
2 1                52       54.9                   60  
3 1                55       59.9                   60  
4 2                53       54.4                   55.5
5 2                54.5     55.4                   55.5

Here is how we could do it:
First use lead to substract 0.1 from lead(start_time)
then for the last value in the groups use an ifelse statement to substract 0.1 from time_end_of_experiment:

library(dplyr)

df %>% 
  group_by(subject_nr) %>% 
  mutate(end_time = lead(start_time, default = last(start_time))-.1, .before=3) %>% 
  mutate(end_time = ifelse(start_time == last(start_time), time_end_of_experiment-.1, end_time))
  subject_nr start_time end_time time_end_of_experiment
  <chr>           <dbl>    <dbl>                  <dbl>
1 1                50       51.9                   60  
2 1                52       54.9                   60  
3 1                55       59.9                   60  
4 2                53       54.4                   55.5
5 2                54.5     55.4                   55.5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文