回答編集履歴

6

薬の並び順によっては意図した出力にならないことがあったためソートを追加

2024/01/12 03:23

投稿

cuttlefish44
cuttlefish44

スコア85

test CHANGED
@@ -21,8 +21,9 @@
21
21
  mutate(seqtime_list = list(tibble(time = time0:time1))) %>%
22
22
  ungroup() %>%
23
23
  unnest(seqtime_list) %>% # nestを解除する (nest列はtibbleなので縦に展開される)
24
+ arrange(id, time, drug) %>% # (追加) 薬の並びがアルファベット順になるようソート
24
25
  group_by(id, time) %>%
25
- summarize(drug = paste(drug, collapse = "+"), .groups = "drop") %>% # ここまで同じ
26
+ summarize(drug = paste(drug, collapse = "+"), .groups = "drop") %>%
26
27
  group_by(id) %>%
27
28
  mutate(lag_drug = lag(drug, default = drug[1]),
28
29
  not_cont_flg = as.numeric(drug != lag_drug),

5

2024/01/12 03:12

投稿

cuttlefish44
cuttlefish44

スコア85

test CHANGED
@@ -20,7 +20,7 @@
20
20
  rowwise() %>% # 行単位で、time0-time1の1刻みtibbleをnest形式で持つ
21
21
  mutate(seqtime_list = list(tibble(time = time0:time1))) %>%
22
22
  ungroup() %>%
23
- unnest(seqtime_list) %>%
23
+ unnest(seqtime_list) %>% # nestを解除する (nest列はtibbleなので縦に展開される)
24
24
  group_by(id, time) %>%
25
25
  summarize(drug = paste(drug, collapse = "+"), .groups = "drop") %>% # ここまで同じ
26
26
  group_by(id) %>%

4

プチ修正

2024/01/12 03:09

投稿

cuttlefish44
cuttlefish44

スコア85

test CHANGED
@@ -21,14 +21,13 @@
21
21
  mutate(seqtime_list = list(tibble(time = time0:time1))) %>%
22
22
  ungroup() %>%
23
23
  unnest(seqtime_list) %>%
24
- select(-c(time0, time1)) %>%
25
24
  group_by(id, time) %>%
26
25
  summarize(drug = paste(drug, collapse = "+"), .groups = "drop") %>% # ここまで同じ
27
26
  group_by(id) %>%
28
27
  mutate(lag_drug = lag(drug, default = drug[1]),
29
28
  not_cont_flg = as.numeric(drug != lag_drug),
30
29
  gr_ind = cumsum(not_cont_flg)) %>% # 同じ薬sでの継続利用グループ番号を作成
31
- group_by(id, drug, gr_ind) %>% # 薬sと継続利用グループ番号でmin, maxを作成
30
+ group_by(id, drug, gr_ind) %>% # 薬sと継続利用グループ番号でmin, maxを作成
32
31
  summarize(time0 = min(time),
33
32
  time1 = max(time),
34
33
  .groups = "drop") %>%

3

2024/01/12 02:53

投稿

cuttlefish44
cuttlefish44

スコア85

test CHANGED
@@ -4,6 +4,17 @@
4
4
 
5
5
  ```
6
6
  library(tidyverse)
7
+
8
+ aaa <- data.frame(id = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4),
9
+ drug = c("A", "B", "C", "B", "C", "A", "A", "B", "C", "D"),
10
+ time0 = c(0, 0, 0, 169, 752, 0, 0, 14, 56, 266),
11
+ time1 = c(214, 432, 381, 190, 988, 28, 267, 564, 57, 688))
12
+
13
+ bbb <- data.frame(id = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4),
14
+ drug = c("A", "B+C", "B", "A", "B", "C", "A", "A+B", "A+B+C","A+B","A+B+D", "B+D", "D"),
15
+ time0 = c(0, 0, 382, 0, 169, 752, 0, 14, 56, 58, 266, 268, 565),
16
+ time1 = c(214, 381, 432, 28, 190, 988, 13, 55, 57, 265, 267, 564, 688))
17
+ # time0の3つめ、time1の2つめについて、typoと判断して修正しました
7
18
 
8
19
  aaa %>%
9
20
  rowwise() %>% # 行単位で、time0-time1の1刻みtibbleをnest形式で持つ

2

期間が排他的になっていなかった問題について修正

2024/01/12 02:42

投稿

cuttlefish44
cuttlefish44

スコア85

test CHANGED
@@ -9,20 +9,34 @@
9
9
  rowwise() %>% # 行単位で、time0-time1の1刻みtibbleをnest形式で持つ
10
10
  mutate(seqtime_list = list(tibble(time = time0:time1))) %>%
11
11
  ungroup() %>%
12
- unnest(seqtime_list) %>% # nest解除
12
+ unnest(seqtime_list) %>%
13
+ select(-c(time0, time1)) %>%
13
14
  group_by(id, time) %>%
14
- summarize(drug = paste(drug, collapse = "+"), .groups = "drop") %>%
15
+ summarize(drug = paste(drug, collapse = "+"), .groups = "drop") %>% # ここまで同じ
15
- group_by(id, drug) %>%
16
+ group_by(id) %>%
17
+ mutate(lag_drug = lag(drug, default = drug[1]),
18
+ not_cont_flg = as.numeric(drug != lag_drug),
19
+ gr_ind = cumsum(not_cont_flg)) %>% # 同じ薬sでの継続利用グループ番号を作成
20
+ group_by(id, drug, gr_ind) %>% # 薬sと継続利用グループ番号でmin, maxを作成
16
21
  summarize(time0 = min(time),
17
22
  time1 = max(time),
18
23
  .groups = "drop") %>%
19
- arrange(id, time0) # 単なる見栄えソート
24
+ arrange(id, time0) %>% # 単なる見栄えソート
25
+ select(-gr_ind)
20
26
 
21
27
  ## 結果
22
- # id drug time0 time1
28
+ # id drug time0 time1
23
- # <dbl> <chr> <int> <int>
29
+ # <dbl> <chr> <int> <int>
24
30
  # 1 1 A 0 214
25
31
  # 2 2 B+C 0 381
26
32
  # 3 2 B 382 432
27
33
  # 4 3 A 0 28
28
34
  # 5 3 B 169 190
35
+ # 6 3 C 752 988
36
+ # 7 4 A 0 13
37
+ # 8 4 A+B 14 55
38
+ # 9 4 A+B+C 56 57
39
+ # 10 4 A+B 58 265
40
+ # 11 4 A+B+D 266 267
41
+ # 12 4 B+D 268 564
42
+ # 13 4 D 565 688

1

2024/01/11 07:03

投稿

cuttlefish44
cuttlefish44

スコア85

test CHANGED
@@ -3,6 +3,8 @@
3
3
  その上で集計するのが最も容易です
4
4
 
5
5
  ```
6
+ library(tidyverse)
7
+
6
8
  aaa %>%
7
9
  rowwise() %>% # 行単位で、time0-time1の1刻みtibbleをnest形式で持つ
8
10
  mutate(seqtime_list = list(tibble(time = time0:time1))) %>%