0
私のdata.frame
には、個々の労働者のログと、病院の特定の病棟で時間を過ごす場所が含まれています。私は今、すべての労働者に沿って、各病棟の一意のIDを含む列を追加したい2つの列に沿ってIDを作成して複製する方法はありますか?
Shift Worker Ward Duration
<fctr> <fctr> <fctr> <dbl>
1 R1 Daniel General 10
2 R1 Daniel General 15
3 R2 Daniel Anaesth 11
4 R2 Daniel Anaesth 13
5 R2 Daniel Anaesth 4
6 R2 Daniel General 15
7 R2 Daniel General 35
8 R2 Daniel Anaesth 6
9 R2 Daniel Anaesth 6
10 R1 Caleb Plastics 10
11 R1 Caleb Plastics 9
12 R1 Caleb Plastics 10
13 R1 Caleb Neuro 9
14 R1 Caleb Neuro 9
15 R1 Caleb Plastics 10
16 R1 Caleb Plastics 10
私はIDが累積することと重複を可能にするものの、次のようにdata.frame
が構成されています。私の予想される出力は次のようになります:
Shift Worker Ward Duration ID
<fctr> <fctr> <fctr> <dbl> <fctr>
1 R1 Daniel General 10 1
2 R1 Daniel General 15 1
3 R2 Daniel Anaesth 11 2
4 R2 Daniel Anaesth 13 2
5 R2 Daniel Anaesth 4 2
6 R2 Daniel General 15 3
7 R2 Daniel General 35 3
8 R2 Daniel Anaesth 6 4
9 R2 Daniel Anaesth 6 4
10 R1 Caleb Plastics 10 1
11 R1 Caleb Plastics 9 1
12 R1 Caleb Plastics 10 1
13 R1 Caleb Neuro 9 2
14 R1 Caleb Neuro 9 2
15 R1 Caleb Plastics 10 3
16 R1 Caleb Plastics 10 3
IDがどのように蓄積されるのでしょうか。どうすればいいですか?
私がこのIDを必要とする理由は、各シフトと労働者に従って、ワードの最初と最後の入力を呼び出すことです。私の予想される出力は次のようになります:
Shift Worker Ward Duration ID
<fctr> <fctr> <fctr> <dbl> <fctr>
1 R1 Daniel General 10 1
2 R1 Daniel General 15 1
3 R2 Daniel Anaesth 11 2
5 R2 Daniel Anaesth 4 2
6 R2 Daniel General 15 3
7 R2 Daniel General 35 3
8 R2 Daniel Anaesth 6 4
9 R2 Daniel Anaesth 6 4
10 R1 Caleb Plastics 10 1
12 R1 Caleb Plastics 10 1
13 R1 Caleb Neuro 9 2
14 R1 Caleb Neuro 9 2
15 R1 Caleb Plastics 10 3
16 R1 Caleb Plastics 10 3
どうすればいいですか? ご協力いただければ幸いです。
'ライブラリ(dplyr)を使用してインデックスを取得することです。 df%>%group_by(Worker)%>%mutate(ID = data.table :: rleid(Ward)) 'または完全なdata.table、' library(data.table); setDT(df)[、ID:= rleid(区)、by =ワーカー] [] ' – alistaire