R语言 如何在初始日期列的基础上将日期插入新列

8aqjt8rx  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(122)

我有一个数据集,其中设置了录音机以进行录音。技术人员记录了部署录音机的日期和时间。然后技术人员将返回并更换电池和存储卡,其中将记录日期。因此,我有每次安装新存储器的日期,或者我认为是录音开始的日期。我正在努力如何填写“date_of_pickup”列基于知道它们是何时按顺序部署的。基本上我知道每个记录器第二次观察的“date_of_deployment”是我需要插入到“date_of_pickup”中的日期。

data <- structure(list(recorder_id = c("smm03978", "smm03895", "smm03922", 
"smm03886", "smm03918", "smm04004", "smm03988", "smm03890", "smm03753", 
"smm03976", "smm03988", "smm04004", "smm03918", "smm03922", "smm03886", 
"smm03976", "smm03895", "smm03753", "smm04004", "smm03753", "smm03890", 
"smm03978", "smm03918", "smm03895", "smm03978", "smm03976", "smm04004", 
"smm03988", "smm03922", "smm03886", "smm03753", "smm03890"), 
    card_id = c("msd-1285\n", "msd-1326", "msd- 0198", "msd-1463", 
    "msd-1478", "msd-0722", "msd-1365", "msd-0807", "msd-0247", 
    "msd-1430", "msd-2683", NA, "msd-2687", "msd-0530", "msd-2682", 
    "msd-2688", "msd-2684", NA, "msd-2826", "msd-2830", "msd-2829", 
    "msd-2828", "msd-2885", "msd-2881", "msd-2878", "msd-2882", 
    "msd-2879", "msd-2877", "msd-2884", "msd-2880", "msd-2886", 
    "msd-2883"), date_of_deployment = c("2022-04-02 13:50:00", 
    "2022-04-02 14:03:00", "2022-04-02 14:41:00", "2022-04-02 14:58:00", 
    "2022-04-02 17:34:00", "2022-04-02 18:27:00", "2022-04-02 18:49:00", 
    "2022-04-03 15:19:00", "2022-04-03 15:32:00", "2022-04-03 16:06:00", 
    "2022-06-24 01:42:00", "2022-06-23 15:44:00", "2022-06-23 16:15:00", 
    "2022-06-23 17:17:00", "2022-06-23 17:32:00", "2022-06-23 18:00:00", 
    "2022-06-23 18:26:00", "2022-06-29 01:25:00", "2022-07-14 13:46:00", 
    "2022-07-15 17:30:00", "2022-07-15 17:48:00", "2022-07-15 18:27:00", 
    "2022-09-10 15:00:00", "2022-09-10 15:27:00", "2022-09-10 15:37:00", 
    "2022-09-10 16:03:00", "2022-09-10 16:38:00", "2022-09-10 16:45:00", 
    "2022-09-10 18:29:00", "2022-09-10 18:38:00", "2022-09-10 19:04:00", 
    "2022-09-10 19:14:00"), date_of_pickup = c("", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "")), row.names = c(NA, 
-32L), class = c("tbl_df", "tbl", "data.frame"))
yi0zb3m4

yi0zb3m41#

library(dplyr)

data |> 
  mutate(date_of_deployment = readr::parse_datetime(date_of_deployment)) |> 
  group_by(recorder_id) |> 
  arrange(date_of_deployment) |> 
  mutate(date_of_pickup = nth(date_of_deployment, 2)) |> 
  ungroup() |> 
  arrange(recorder_id)

# A tibble: 32 × 4
   recorder_id card_id  date_of_deployment  date_of_pickup     
   <chr>       <chr>    <dttm>              <dttm>             
 1 smm03753    msd-0247 2022-04-03 15:32:00 2022-06-29 01:25:00
 2 smm03753    NA       2022-06-29 01:25:00 2022-06-29 01:25:00
 3 smm03753    msd-2830 2022-07-15 17:30:00 2022-06-29 01:25:00
 4 smm03753    msd-2886 2022-09-10 19:04:00 2022-06-29 01:25:00
 5 smm03886    msd-1463 2022-04-02 14:58:00 2022-06-23 17:32:00
 6 smm03886    msd-2682 2022-06-23 17:32:00 2022-06-23 17:32:00
 7 smm03886    msd-2880 2022-09-10 18:38:00 2022-06-23 17:32:00
 8 smm03890    msd-0807 2022-04-03 15:19:00 2022-07-15 17:48:00
 9 smm03890    msd-2829 2022-07-15 17:48:00 2022-07-15 17:48:00
10 smm03890    msd-2883 2022-09-10 19:14:00 2022-07-15 17:48:00
# … with 22 more rows
# ℹ Use `print(n = ...)` to see more rows

编辑:也许你想要mutate(date_of_pickup = lead(date_of_deployment))而不是mutate(date_of_pickup = nth(date_of_deployment, 2))?这将导致以下输出:

# A tibble: 32 × 4
   recorder_id card_id  date_of_deployment  date_of_pickup     
   <chr>       <chr>    <dttm>              <dttm>             
 1 smm03753    msd-0247 2022-04-03 15:32:00 2022-06-29 01:25:00
 2 smm03753    NA       2022-06-29 01:25:00 2022-07-15 17:30:00
 3 smm03753    msd-2830 2022-07-15 17:30:00 2022-09-10 19:04:00
 4 smm03753    msd-2886 2022-09-10 19:04:00 NA                 
 5 smm03886    msd-1463 2022-04-02 14:58:00 2022-06-23 17:32:00
 6 smm03886    msd-2682 2022-06-23 17:32:00 2022-09-10 18:38:00
 7 smm03886    msd-2880 2022-09-10 18:38:00 NA                 
 8 smm03890    msd-0807 2022-04-03 15:19:00 2022-07-15 17:48:00
 9 smm03890    msd-2829 2022-07-15 17:48:00 2022-09-10 19:14:00
10 smm03890    msd-2883 2022-09-10 19:14:00 NA                 
# … with 22 more rows
# ℹ Use `print(n = ...)` to see more rows

相关问题